如何使我的按钮响应点击

时间:2018-08-28 09:32:36

标签: python user-interface button tkinter

我想为桌面应用程序创建Python GUI。我是用Tkinter设计的:

两个按钮用于执行命令并退出应用程序,Button one进入代码,Button two退出。但是GUI没有响应。

执行过程时,按button two不会退出应用程序,按button 1不会触发功能def clicked()中的代码。

即使在运行程序时,是否还有更好的退出方法(例如中断?)

在Tkinter中,我们可以不按按钮就传递值。 (过程在重复,但是我只需要按一下按钮,需要在显示GUI时自动执行)

def GUI():

   window =Tk()
   window.title("Select First Color")
   selected = IntVar()   

   rad1 = Radiobutton(window, text='RED', value=1, variable=selected)     
   rad2 = Radiobutton(window, text='YELLOW', value=2, variable=selected)

        def clicked():   
             # Go to a function according to the selection of radio button

   button1 = Button(window, text="Select", command=clicked)                    
   button2 = Button(window, text="Quit", command=window.destroy)

   rad1.grid(column=0, row=0)              # GUI RadioButton Placement
   rad2.grid(column=1, row=0)
   button1.grid(column=6, row=0)               # GUI Button Placement
   button2.grid(column=6, row=1)

window.mainloop()

3 个答案:

答案 0 :(得分:1)

Tkinter是单线程的,这意味着它一次只能做一件事。如果您的函数clicked需要很长时间,则tkinter在运行时将无法响应事件。

您唯一的选择是将clicked完成的工作移到单独的线程或进程中,或者对其进行重构,使其定期调用update方法,从而为tkinter提供工作机会通过事件队列。

请参阅此问题以获取更多帮助:Tkinter: How to use threads to preventing main event loop from "freezing"

答案 1 :(得分:0)

我认为这就是您要寻找的

button2 = Button(window, text="Quit", command=sys.exit)

答案 2 :(得分:0)

1-您永远不会打电话给GUI
2-在函数内部定义的变量具有局部范围,除非在全局范围内声明它们并使用global更改其值,否则无法在外部访问它们。

以下内容符合您的期望:

未选择任何单选按钮时,click打印0
当选择“红色”单选按钮时,click打印1
当选择“黄色”按钮时,click打印2
您可以使用这些值将调用重定向到另一个函数。

import tkinter as tk


def do_nothing():
    pass


def do_red():
    print('doing the red thing')


def do_yellow():
    print('doing the yellow thing')


def clicked():
    print(f'clicked {selected.get()}')
    v = selected.get()
    if v == 0:
        do_nothing()
    elif v == 1:
        do_red()
    elif v == 2:
        do_yellow()
    else:
        print('an error occurred, the wrong value was recieved')


def GUI():

    global rad1, rad2, selected

    window = tk.Tk()
    window.title("Select First Color")
    selected = tk.IntVar()   

    rad1 = tk.Radiobutton(window, text='RED', value=1, variable=selected)     
    rad2 = tk.Radiobutton(window, text='YELLOW', value=2, variable=selected)

    button1 = tk.Button(window, text="Select", command=clicked)                    
    button2 = tk.Button(window, text="Quit", command=window.destroy)

    rad1.grid(column=0, row=0)
    rad2.grid(column=1, row=0)
    button1.grid(column=6, row=0)
    button2.grid(column=6, row=1)

    window.mainloop()


rad1, rad2, selected = None, None, None
GUI()

或者,您可以使用类以避免全局变量:

import tkinter as tk


def do_nothing():
    pass


def do_red():
    print('doing the red thing')


def do_yellow():
    print('doing the yellow thing')


class GUI(tk.Tk):

    def __init__(self):
        super().__init__()
        self.title("Select First Color")
        self.selected = tk.IntVar()
        self.selected.set(0)

        self.rad1 = tk.Radiobutton(self, text='RED', value=1, variable=self.selected)     
        self.rad2 = tk.Radiobutton(self, text='YELLOW', value=2, variable=self.selected)

        self.button1 = tk.Button(self, text="Select", command=self.clicked)                    
        self.button2 = tk.Button(self, text="Quit", command=self.destroy)

        self.rad1.grid(column=0, row=0)
        self.rad2.grid(column=1, row=0)
        self.button1.grid(column=6, row=0)
        self.button2.grid(column=6, row=1)

        self.mainloop()

    def clicked(self):
        print(f'clicked {self.selected.get()}')

        v = self.selected.get()
        if v == 0:
            do_nothing()
        elif v == 1:
            do_red()
        elif v == 2:
            do_yellow()
        else:
            print('an error occurred, the wrong value was received')


GUI()