2016-12-25 80 views
0

下面的代碼:Python的類型錯誤:take_one()缺少必需的1位置參數: '狀態'

from tkinter import * 

class Main_content: 
    def take_one(self, state): 
     state = state - 1 
     return self.state 

    def take_two(self, state): 
     state = state - 2 
     return self.state 

    def __init__(self, master, state): 
     frame = Frame(master) 
     frame.pack() 

     self.label_status = Label(frame, text = "Coins left: " + str(state)) 
     self.label_status.pack() 
     self.label_status = Label(frame, text = "Player " + str(player) + " turn's") 
     self.label_status.pack() 

     self.pak_een = Button(frame, text="Take 1 coin", command=self.take_one) 
     self.pak_een.pack() 
     self.pak_twee = Button(frame, text="Take 2 coins", command=self.take_two) 
     self.pak_twee.pack() 

root = Tk() 

player = 1 
state = 21 
b = Main_content(root, state) 

root.mainloop() 

的錯誤是:

Traceback (most recent call last): 
    File "C:\Users\Freddie\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__ 
    return self.func(*args) 
TypeError: take_two() missing 1 required positional argument: 'state' 

我新,所以我想不通我做錯了什麼。我不知道這意味着什麼。

+0

按鈕不會nd事件或其他參數給他們的命令處理程序。你的論點「國家」應該從哪裏來? – Marvo

+0

@DeepSpace爲什麼要刪除traceback標籤和python3標籤?有沒有用? – Stargateur

+1

@Stargateur這個錯誤不是Python3特有的,跟蹤標記與這個問題無關。 – DeepSpace

回答

2

有一些問題:

  1. take_onetake_two方法需要一個參數state,但你永遠不通過他們這樣的說法。

  2. self.state您試圖在這些函數中返回的內容從未在任何地方定義過。

  3. 即使代碼的其餘部分工作,行state = state - 1state = state - 2沒有任何用處。 state是這些函數中的局部變量,永遠不會被返回或使用。

,可以儲存state傳遞給__init__self.state,則該事件處理程序將有機會獲得它,不需要把它作爲一個參數,他們將不再需要返回它:

class Main_content: 

    def take_one(self): 
     self.state -= 1 

    def take_two(self): 
     self.state -= 2 

    def __init__(self, master, state): 
     frame = Frame(master) 
     frame.pack() 

     self.state = state 
     self.label_status = Label(frame, text = "Coins left: " + str(self.state)) 
     self.label_status.pack() 
     self.label_status = Label(frame, text = "Player " + str(player) + " turn's") 
     self.label_status.pack() 

     self.pak_een = Button(frame, text="Take 1 coin", command=self.take_one) 
     self.pak_een.pack() 
     self.pak_twee = Button(frame, text="Take 2 coins", command=self.take_two) 
     self.pak_twee.pack() 

如果我按照你的邏輯,你還需要每一個按鈕按下的self.state電流值的時間來更新標籤的價值,我會離開,作爲一個練習留給讀者:)

+0

是的,每次玩家按下一個按鈕都必須更新標籤,但是當我運行這段代碼時,它什麼也不做 – Freddie

相關問題