gpt4 book ai didi

python - 如何关闭 Tkinter 窗口

转载 作者:行者123 更新时间:2023-12-01 02:40:26 24 4
gpt4 key购买 nike

我想生成一个带有两个按钮的窗口,这两个按钮都会执行一个函数,然后关闭窗口。但是,我似乎无法关闭窗口。我已经根据类似问题的答案多次尝试用函数重写,但每次都会出错。任何帮助将不胜感激!

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()

最佳答案

添加 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()

关于python - 如何关闭 Tkinter 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45766851/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com