Tkinter GUI,图和线程

时间:2019-07-23 14:36:21

标签: python tkinter

我正在尝试使用Tkinter GUI的后台进程来显示绘图。但是,当我按一个按钮开始该过程时,GUI“冻结”。

我已启用将功能绘制为后台进程的进程,并且制作了GUI。请参见下面的代码。我启用了一个非常简单的绘图和一个非常简单的GUI。绘图线程通过后台进程运行。

import tkinter as tk
from pandas import DataFrame
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import threading

def functionPlot():
    Data = {'Country': ['USA', 'UK', 'CAN', 'GER', 'FR', 'FIN', 'SWE'],
            'GDP_Per_Capita': [45000, 49000, 42000,56000, 47000, 58000, 57000]}

    df = DataFrame(Data, columns = ['Country', 'GDP_Per_Capita'])
    df = df[['Country', 'GDP_Per_Capita']].groupby('Country').sum()
    print(df)
    for i in range(0, 20):
        print('Hello')
    df['GDP_Per_Capita'].plot(kind = 'bar')
    plt.ion()

def myLongProcess(isRunningFunc=None):
    print("Starting My Long Process")
    for i in range(1, 10):
        try:
            if not isRunningFunc():
                self.onMyLongProcessUpdate("Stopped!")
                return
        except: pass
        self.onMyLongProcessUpdate(i)
        time.sleep(1.5)
    self.onMyLongProcessUpdate("Done!")

def onMyLongProcessUpdate(status):
    print("Process Update: %s" % (status, ))

class BackgroundTask():
    def __init__(self, taskFuncPointer):
        self.__taskFuncPointer_=taskFuncPointer
        self.__workerThread_=None
        self.__isRunning_=False

    def taskFuncPointer(self):return self.__taskFuncPointer_

    def isRunning(self):
        return self.__isRunning_ and self.__workerThread_.isAlive()

    def start(self):
        if not self.__isRunning_:
            self.__isRunning_=True
            self.__workerThread_ = self.WorkerThread(self)
            self.__workerThread_.start()
    def stop(self): self.__isRunning_=False

    class WorkerThread(threading.Thread):
        def __init__(self, bgStatusTask):
            threading.Thread.__init__(self)
            self.__bgStatusTask_ = bgStatusTask

        def run(self):
            try:
                self.__bgStatusTask_.taskFuncPointer()
                (self.__bgStatusTask_.isRunning)
            except Exception as e: print(repr(e))
            self.__bgStatusTask_.stop()


root = tk.Tk()
bgStatusTask = BackgroundTask(functionPlot)
bgStatusTask.start()

menu = tk.Menu(root)
fileMenu = tk.Menu(root)
menu.add_cascade(label="File", menu=fileMenu, underline=0)
fileMenu.add_cascade(label = "Open Log File")
fileMenu.add_cascade(label = "Reset Status")

helpMenu = tk.Menu(root)
menu.add_cascade(label="Help", menu=helpMenu, underline=0)
helpMenu.add_cascade(label="Help Document")
helpMenu.add_cascade(label="About This Program")

b1 = tk.Button(root, text="Click Me!", fg="red",command=functionPlot)
b1.pack()

root.configure(menu = menu, bg = "blue")
root.geometry('400x400')
root.mainloop()

1 个答案:

答案 0 :(得分:0)

使用constructor(@Inject(SESSION_STORAGE) private storage: WebStorageService) { this.storage.set('filters', JSON.stringify(this.filters)); } 而不是this.storage.get('filters'); 时,在外部窗口中显示图没有问题

要在窗口内显示,请使用此代码。但是我删除了线程。

plt.show()