如何组织线程GUI应用程序(Python)

时间:2013-05-31 11:23:49

标签: python user-interface tkinter queue python-multithreading

我无法使用Tkinter(和ttk)和Python将我的代码组织成一个可用而不是超级错误的程序。基本上,它现在只是从网上下载图像,但即使是简单的GUI,我也遇到了麻烦。虽然一切都在控制台中运行,但制作GUI是一场噩梦,更不用说让它工作了。好吧,现在我有它工作,但它经常崩溃,很明显,我做错了错误的GUI中的变量错误被正确访问(甚至控制台中的错误消息,我已经自己放置功能,以确保事情去正确)和不断崩溃。

基本上我有类似的东西。

发生和需要工作的主要事情是:用户输入字符串从entrytext发送到程序的密集部分(当前包含在线程中),密集部分步进GUI的进度条,密集部分发送文本没有GUI和密集部分崩溃的消息到文本框/记录器。此外,密集部分应在GUI完全加载后立即启动,并在准备好时将启动消息发送到文本框。

密集部分处理其他事情,但不会干扰GUI,例如实际下载和保存图像,浏览和文件I / O,而且我也没有任何问题,无论如何。

我还阅读了有关队列和线程以及教程的内容,但我似乎还是没有得到它。特别是我将如何让程序不断地在GUI中逐步显示进度条,同时还向GUI发送文本消息(我甚至可以从队列中进行操作,例如,无需执行非常慢的CPU和密集型的While和If循环和多个使得它更加疯狂的队列。在简单的例子中,只需要一个简单的while和queue.get()等待,因为它消耗很少的资源。所以我的问题是,我需要为此实现什么样的结构,如果可能的话,我可以得到一两个例子(我从示例中学到的东西比从阅读文档中学到的更好)?非常感谢你。

from Tkinter import *
import ttk
import Threading
import #a bunch of other stuff

class myHardWorkerThread (threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self.myClass = ModifiedConsoleClass()

    def run(self):
            #thread needs to wait at least a little otherwise the thread begins too
            #fast and causes even more errors, probably due to it sending text to
            #the textbox upon startup among other things and just overall no
            #organization
            time.sleep(3)
            self.myClass.BeginDoingStuff()

class ApplyMyGuiAndStartThread():
    def __init__(self, root, thread):

        root.geometry('500x500')
        root.resizable(0,0)

        #Put backgrounds or images or logos  here
        self.canvas = Canvas(root)
        self.canvas.pack()

        #My textbox that acts like a Log/Console output
        self.txtLogger = Text(root,state="disabled",wrap="word")
        self.txtLogger.place()
        self.scrollbar = Scrollbar(root)
        self.scrollbar.place()

        #Progressbar
        self.myVal = IntVar()
        self.TProgressbar = ttk.Progressbar(root, orient=HORIZONTAL, variable = self.myVal, mode='determinate')
        self.TProgressbar.place()

        #Entrybox for user input
        self.txbEntryText = StringVar()
        self.txtbEntry = ttk.Entry (root, textvariable=self.txbEntryText)
        self.txtbEntry.place()
        self.txtbEntry.bind("<Return>", self.SendFromGUItoThread)

        self.thread = thread
        self.thread.start()

    def SendFromGUItoThread(self,arg=None):

        myentry = str(self.txtbEntry.get())
        self.txtbEntry.delete(0, END)
        self.thread.myClass.entryBoxValueCopy = myentry


    def SendFromThreadToGUI(self,msg):
        try:
            self.txtLogger['state'] = 'normal'
            self.txtLogger.insert('end', msg)
            self.txtLogger['state'] = 'disabled'
        except:
            print "Could not be printed"


class ModifiedConsoleCode():
    def __init__(self):
        #constants here like
        self.entryBoxValueCopy = None

    def BeginDoingStuff():
        #Thread does the bulk of work  here, includes connecting to websites,
        #collecting info, sending text messages to GUI, downloading images and
        #stepping the progressbar by a calculated amount by file size

    def OneOfManyFunctionsUsedInsideBeginDoingStuff():
        #Breaks things down like looping time.sleep waits for user input in the entry box
        #showing up in entryBoxValueCopy, and using the user input to surf websites and
        #collect images

if __name__ == '__main__':

        root = Tk()
        root.title(titleOfTheProgram)

        worker = myHardWorkerThread()

        w = ApplyMyGuiAndStartThread(root,worker)

        root.mainloop()
        os._exit(0)

3 个答案:

答案 0 :(得分:3)

简短的回答是,您无法与工作线程中的小部件进行交互。你唯一的选择是让你的工作线程在一个线程安全的队列上推送一些东西,并让主线程轮询它。

您不需要任何while循环来轮询队列。你已经有了一个无限循环 - 事件循环(例如:mainloop) - 所以不需要添加额外的循环。

从主线程轮询队列的方法如下所示:

def pollQueue(self):
    <look at the queue, act on the results>
    self.after(100, self.pollQueue)

这样做是安排每100毫秒轮询一次队列。当然,您可以将轮询间隔设置为您想要的任何值。

答案 1 :(得分:1)

不应使用线程,而应使用tkinter方法“after”在tkinter事件循环上设置事件。

例如,当使用canvas元素时,我会使用

canvar.after(50, func=keepDoingSomething)

这类似于 javascript 函数setTimeout,它是线程安全的,不会干扰tkinter gui线程。

答案 2 :(得分:0)

我认为最好创建3个类而不是2个类并将其划分为

  • GUI
  • 功能
  • 应用

GUI和功能非常自我描述,该应用程序是两者之间的桥梁,因此不会妨碍其工作。

示例工作代码是这个

import tkinter as tk
from tkinter import ttk,messagebox
import threading
import time

#base GUI Class
class GUI:
    def __init__(self, root, runCommand):
        mf = ttk.Frame(root, padding="5 5 5 5")
        mf.grid(column=0, row=0)
        mf.columnconfigure(0, weight=1)
        mf.rowconfigure(0, weight=1)

        # Global Values
        self.Fnm = tk.StringVar(root, "SearchFile.xlsx")
        self.Ncol = tk.StringVar(root, "D")
        self.Vcol = tk.StringVar(root, "C")
        # Label
        tk.Label(mf, text="File Name").grid(column=1, row=1, pady=6)
        tk.Label(mf, text="Name Col").grid(column=1, row=3, pady=6)
        tk.Label(mf, text="Value Col").grid(column=3, row=3, pady=6)

        # components
        self.fname = ttk.Entry(mf, width=18, textvariable=self.Fnm)
        self.nmCol = ttk.Entry(mf, width=6, textvariable=self.Ncol)
        self.valCol = ttk.Entry(mf, width=6, textvariable=self.Vcol)
        self.but = ttk.Button(mf, text="Refresh", command=runCommand)
        self.pgbar = ttk.Progressbar(mf, orient="horizontal", mode="determinate")

        # Design
        self.fname.grid(column=2, row=1, pady=3, columnspan=3)
        self.nmCol.grid(column=2, row=3, pady=3)
        self.valCol.grid(column=4, row=3, pady=3)
        self.but.grid(column=2, row=2, columnspan=2)
        self.pgbar.grid(column=1,row=4,columnspan=4)

    def refresh(self):
        pass

    def get(self):
        return [self.Fnm.get(), self.Ncol.get(), self.Vcol.get()]

#Base process Class
class Proc:
    def __init__(self, dets,pgbar,but):
        self.Fnm = dets[0]
        self.Ncol = dets[1]
        self.Vcol = dets[2]
        self.pg=pgbar
        self.butt=but

    def refresh(self):
        self.butt['state'] = 'disabled'
        self.pg.start()
        #ATTENTION:Enter Your process Code HERE
        for _ in range(5):
            time.sleep(2)
        self.pg.stop()
        #Any search/sort algorithm to be used
        #You can use self.pg.step() to be more specific for how the progress bar proceeds
        messagebox.showinfo("Process Done","Success")
        self.butt['state'] = 'enabled'

#Base Application Class
class App:
    def __init__(self, master):
        self.master = master
        self.gui = GUI(self.master, self.runit)

    def runit(self):
        self.search = Proc(self.gui.get(),self.gui.pgbar,self.gui.but)
        self.thread1 = threading.Thread(target=self.search.refresh)
        self.thread1.start()

def main():
    app = tk.Tk()
    gui = App(app)
    app.title("Refresh Search File")
    app.mainloop()

if __name__ == '__main__':
    main()