从单独的类配置tkinter对象

时间:2015-04-09 14:01:44

标签: python python-2.7 tkinter tk tkinter-canvas

我有一个带有两个类的简单程序,一个通过serial.serial连接控制一个继电器板。另一个类用于GUI,它将命令发送到继电器类,然后显示继电器板的状态。

我遇到了从中继类向tkinter类发送消息的问题。只有在relay命令完成后才会显示消息。我已经减少了下面的程序。 Test.test()表示我的中继类中的一个函数,其中MainWindow类是我的GUI。

Someone已指出使用threading来处理类之间传递的消息。这是我唯一的选择吗?我还没有钻研过线程。

from Tkinter import *
import time
import ScrolledText

class Test():
     def test(self):
        main.textboxupdate(" test start ")
        time.sleep(2)
        main.textboxupdate(" test middle ")
        time.sleep(2)
        main.textboxupdate(" test end ")

class MainWindow(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)       
        self.canvas = Canvas(width=1200,height=700)
        self.canvas.pack(expand=YES,fill=BOTH)
        self.frame = Frame(self.canvas)
        self.TextBox = ScrolledText.ScrolledText(self.frame) 
        self.open = Button(self.frame, text="Open Cover",
                            command=test.test)

    def createtextbox(self, statusmsg):
        self.frame.place(x=0,y=0)
        self.TextBox.config(state = NORMAL)
        self.TextBox.insert(END, statusmsg,('error'))
        self.TextBox.config(state = 'disabled', height = 2, width = 35)
        self.TextBox.see(END)
        self.TextBox.grid(columnspan=2, rowspan = 1)
        self.open.grid()

    def textboxupdate(self, statusmsg):
        statusmsg =  statusmsg +'\n'
        self.TextBox.config(state = NORMAL)
        self.TextBox.insert(END, statusmsg,('error'))
        self.TextBox.config(state = 'disabled', height = 10, width = 50)
        self.TextBox.see(END)  

test = Test()        
root = Tk()  
main = MainWindow(root)
main.createtextbox('Startup\n')
root.mainloop()

1 个答案:

答案 0 :(得分:0)

这是一个选项:

from Tkinter import *
import time
import ScrolledText
import threading, Queue

class Test():
    def __init__(self):
        self.msg_queue = Queue.Queue()

    def test(self):
        self.msg_queue.put(" test start ")
        time.sleep(2)
        self.msg_queue.put(" test middle ")
        time.sleep(2)
        self.msg_queue.put(" test end ")

class MainWindow(Frame):
    def __init__(self, *args, **kwargs):
        Frame.__init__(self, *args, **kwargs)       
        self.canvas = Canvas(width=1200,height=700)
        self.canvas.pack(expand=YES,fill=BOTH)
        self.frame = Frame(self.canvas)
        self.TextBox = ScrolledText.ScrolledText(self.frame) 
        self.open = Button(self.frame, text="Open Cover",
                            command=self.create_thread)
        self.test_thread = None
        self.createtextbox("")

    def create_thread(self):
        self.test_thread = threading.Thread(target=test.test)
        self.test_thread.start()
        self.after(10, self.update_textbox)

    def update_textbox(self):
        while not test.msg_queue.empty():
            self.textboxupdate(test.msg_queue.get())
        if self.test_thread.is_alive():
            self.after(10, self.update_textbox)
        else:
            self.test_thread = None


    def createtextbox(self, statusmsg):
        self.frame.place(x=0,y=0)
        self.TextBox.config(state = NORMAL)
        self.TextBox.insert(END, statusmsg,('error'))
        self.TextBox.config(state = 'disabled', height = 2, width = 35)
        self.TextBox.see(END)
        self.TextBox.grid(columnspan=2, rowspan = 1)
        self.open.grid()

    def textboxupdate(self, statusmsg):
        statusmsg =  statusmsg +'\n'
        self.TextBox.config(state = NORMAL)
        self.TextBox.insert(END, statusmsg,('error'))
        self.TextBox.config(state = 'disabled', height = 10, width = 50)
        self.TextBox.see(END)  
        self.update_idletasks()

test = Test()
main = MainWindow()
main.pack()
main.mainloop()

第一个更改是,Test.test不是调用函数,而是将消息放入队列。 Test.test在单独的帖子中MainWindow.start_thread启动MainWindow.start_threadupdate_textbox还通过要求tkinter在10毫秒(self.after(10, self.update_textbox))之后调用MainWindow来安排对线程的检查。此函数将所有新消息从队列中删除,并显示它们。然后,如果线程仍在运行,它会自行重新安排,否则会重置{{1}}。