所以我正在为Tkinter程序测试一些代码,并希望有一个主菜单,然后是游戏本身。
我想将主菜单放在__init__()
函数中,然后当按下按钮时,它会调用命令删除主菜单并加载新的小部件。为了测试我只有2个按钮作为我的小部件,只是为了解决这个想法。但它出现了一个错误:
from tkinter import *
from tkinter import ttk
class Application:
def __init__(self, master):
But = ttk.Button(master, text = "Play", command = self.Play).pack()
def Play(self, master):
self.But.destroy()
ttk.Button(master, text = "Test Button").pack()
def main():
root = Tk()
Menu = Application(root)
root.resizable(width=False, height=False)
root.mainloop()
if __name__ == "__main__": main()
单击“播放”按钮时,会发生以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "D:\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
TypeError: Play() missing 1 required positional argument: 'master'
我很擅长使用类进行tkinter编程。任何帮助都会很棒。
答案 0 :(得分:1)
你得到了这个错误,因为当你定义了master
函数(在ttk.Button
中)时,你已经指定了一个名为lambda:self.Play(master)
的位置参数,这是强制性的,但是当你使用它时你忽略了它与import tkinter as tk
from tkinter import ttk
class Application:
def __init__(self, master):
self.master = master
self.button = ttk.Button(self.master, text = "play", command = self.play)
self.button.pack()
def play(self):
self.button.destroy()
self.new_button = ttk.Button(self.master, text = "Test Button")
self.new_button.pack()
def main():
root = tk.Tk()
Menu = Application(root)
root.resizable(width=False, height=False)
root.mainloop()
if __name__ == "__main__":
main()
小部件的回调功能相同。这意味着您应该以这种方式编写代码:select type, count(*)
from (
select idxdaftar, case when count(distinct kode) = count(distinct case when kode = 1 then kode end) then '1' else '1+2' end type
from (
select 50 idxdaftar, 1 kode
union all
select 51 idxdaftar, 1 kode
union all
select 50 idxdaftar, 2 kode
) tab
group by idxdaftar
having count(distinct kode) in (
count(distinct case when kode = 1 then kode end),
count(distinct case when kode in (1,2) then kode end))
) t
group by type
。
一旦你这样做,Python解释器会通知你,你在程序的其他地方犯了其他错误。因此,让我在PEP8之后为您的计划提供最终解决方案:
{{1}}