如何关闭风

2024-04-25 21:56:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想生成一个有两个按钮的窗口,这两个按钮都将执行一个函数,然后关闭窗口。不过,我好像关不上窗户。我试过根据类似问题的答案用函数重写了几次,但每次都会出错。任何帮助将不胜感激!在

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

Tags: 函数texttestselfrightboxdefbutton
1条回答
网友
1楼 · 发布于 2024-04-25 21:56:48

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

相关问题 更多 >

    热门问题