如何解决程序执行大循环时持续按下的tkinter按钮?

时间:2017-10-23 02:55:34

标签: python tkinter

实际上,我想创建一个通过串口接收数据的GUI,但我遇到的问题是按钮会不断按下,直到整个数据整理完成。

为了让人们了解我遇到的情况,我改变了下面的代码

from Tkinter import *

class GUIDemo(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets()
        # self.datarcv()

    def createWidgets(self):
        self.inputText = Label(self)
        self.inputText["text"] = "Input:"
        self.inputText.grid(row=0, column=0)

        self.inputField = Entry(self)
        self.inputField["width"] = 50
        self.inputField.grid(row=0, column=1, columnspan=6)

        self.regist = Button(self, command=self.datasave)
        self.regist["text"] = "button"
        self.regist.grid(row=2, column=0)

    def datasave(self):
        a=0
        while a<100000000:
            a+=1
            print a



if __name__ == '__main__':
    root = Tk()
    app = GUIDemo(master=root)
    app.mainloop()

1 个答案:

答案 0 :(得分:1)

您需要添加一个帖子

from Tkinter import *
import threading

class GUIDemo(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.grid()
        self.createWidgets()
        # self.datarcv()

    def createWidgets(self):
        self.inputText = Label(self)
        self.inputText["text"] = "Input:"
        self.inputText.grid(row=0, column=0)

        self.inputField = Entry(self)
        self.inputField["width"] = 50
        self.inputField.grid(row=0, column=1, columnspan=6)

        self.regist = Button(self, command=self.datasave_thread)
        self.regist["text"] = "button"
        self.regist.grid(row=2, column=0)

    def datasave_thread(self):
        self.thread = threading.Thread(None, self.datasave, None, (), {})
        self.thread.start()

    def datasave(self):
        a=0
        while a<100000000:
            a+=1
            print a



if __name__ == '__main__':
    root = Tk()
    app = GUIDemo(master=root)
    app.mainloop()