![](/img/trans.png)
[英]TypeError: endturn() missing 1 required positional argument: 'self'
[英]TypeError: exe() missing 1 required positional argument: 'self'
我知道本網站上有解決此問題的答案,但我遇到的所有解決方案似乎都對我的情況沒有幫助。 我正在使用 Tkinter(並試圖學習如何使用它)來制作游戲。 我想要一個按鈕(退出游戲)退出 Tkinter 窗口,但我不斷收到此錯誤:
類型錯誤:exe() 缺少 1 個必需的位置參數:'self'
我的代碼:
from tkinter import *
import sys as s, time as t
try: color = sys.stdout.shell
except AttributeError: raise RuntimeError("This programme can only be run in IDLE")
color.write(" | Game Console | \n", "STRING")
root = Tk()
root.title("Tic-Tac-Toe")
menuFrame = Frame(root)
menuFrame.pack()
buttonFrame = Frame(root)
buttonFrame.pack()
Label(menuFrame, text = ("Tic-Tac-Toe"), font = ("Helvetica 12 bold")).grid(row = 10, column = 5)
def play():
color.write("Game window opening... \n", "STRING")
#open new window to game later
def exe(self):
color.write("Goodbye for now \n", "STRING")
self.destroy()
playButton = Button(buttonFrame, text = ("Play game"), command = play)
playButton.pack(side=LEFT)
Button(root, text=("Quit Game"), command=exe).pack()
root.mainloop()
我似乎無法找到它的含義,因為我已將其定義到函數中。 預先感謝您的解決方案。
在您的代碼中,您有:
def exe(self):
這意味着您需要self
參數; self
與類的實例一起使用,並且由於您的方法沒有類,您可以像這樣在方法標題中省略self
參數,如下所示:
def exe():
color.write("Goodbye for now \n", "STRING")
root.destroy()
您的方法以要求(位置)參數self
的方式定義。 它通常用作類的對象引用,但由於您沒有類,它現在只是一個常規參數,您沒有通過。 您可以通過使用匿名函數(lambda)來傳遞該參數,替換:
Button(..., command=exe).pack()
和:
Button(..., command=lambda widget=root: exe(widget)).pack()
您還應該更好地替換:
def exe(self):
...
self.destroy()
和:
def exe(widget_to_be_destroyed):
...
widget_to_be_destroyed.destroy()
為消歧。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.