2017-01-09 120 views
-1

對於我需要製作nim遊戲的項目,必須有一個標籤來顯示它是哪個玩家。但是現在我需要在玩家點擊一個按鈕以獲取1或2個硬幣後更新標籤。必須有2名球員,現在我不知道該怎麼做。更新顯示tkinter玩家的標籤

這裏是我的腳本:

from tkinter import * 

player = 1 

def player_status(): 
    global player 

root = Tk() 
root.geometry('500x300') 

frame = Frame(root) 

state = Label(frame, text="State: " + str(coins)) 
state.pack() 
player = Label(frame, text="Player " + str(player) + " turns!") 
player.pack() 

takeonecoin = Button(frame, text="1 coin", commad=one_coin) 
takeonecoin.pack() 
taketwocoins = Button(frame, text="2 coins", command=two_coins) 
taketwocoins.pack() 

frame.pack() 

root.mainloop() 
+1

你的代碼看起來很亂,我會引用你去學習ab在創建遊戲時,幾乎所有的對象和類都是必需的,即使是簡單的遊戲也是如此。 – dannyxn

+1

首先定義一個'Player'類並創建它的兩個實例。然後將其中的一個指定爲'current_player',並修改其餘的函數以引用(作爲參數或全局變量)。這將允許你簡單地通過給'current_player'分配一個不同的值來切換玩家。 – martineau

+1

兄弟,我檢查了你的代碼,它甚至不工作! – dannyxn

回答

-1

你需要使用 「綁定」 選項:

takeonecoin = Button(frame, text="1 coin") 
takeonecoin.bind('<Button-1>', one_coin) 
takeonecoin.pack() 
taketwocoins = Button(frame, text="2 coins") 
taketwocoins.bind('<Button-1>', two_coin) 
taketwocoins.pack() 

它是: xxx.bind(,命令應執行)

例如我的第一個答案,對不起我的英文

+0

當我這樣做比我得到這個錯誤= TypeError:one_coin()需要0位置參數,但1給出 –

+0

@FreddieBo你需要定義函數像def one_coin(事件): –

+0

現在它的工作。 –