我想生成一个带有两个按钮的窗口,这两个按钮都会执行一个函数,然后关闭窗口。但是,我似乎无法让窗户关闭。我根据类似问题的答案尝试了几次函数重写,但每次都会出错。任何帮助将不胜感激!
import tkinter as tk
class test:
def __init__(self, root):
self.text = tk.Label(root, text = 'Question' )
self.text.pack(side = 'top')
self.button1 = tk.Button(root, text = 'Yes', command = self.write_right, width = 15)
self.button1.pack(side='left')
self.button2 = tk.Button(root, text = 'No', command = self.write_wrong, width = 15)
self.button2.pack(side='right')
def write_right(self):
self.root.destroy()
def write_wrong(self):
self.root.destroy()
box = tk.Tk()
functionality = test(box)
box.mainloop()
答案 0 :(得分:2)
添加self.root = root
可以提供帮助:
import tkinter as tk
class test:
def __init__(self, root):
self.root = root
self.text = tk.Label(root, text = 'Question' )
self.text.pack(side = 'top')
self.button1 = tk.Button(root, text = 'Yes', command = self.write_right, width = 15)
self.button1.pack(side='left')
self.button2 = tk.Button(root, text = 'No', command = self.write_wrong, width = 15)
self.button2.pack(side='right')
def write_right(self):
self.root.destroy()
def write_wrong(self):
self.root.destroy()
box = tk.Tk()
functionality = test(box)
box.mainloop()