如何阻止Tkinter窗口冻结?

时间:2020-05-30 12:36:17

标签: python multithreading tkinter

下面是我用来运行两个python文件的主要功能,但是一旦单击按钮后,我的窗口就会冻结。请告诉我一种执行多威胁处理的方法,以便可以一次单击两个按钮。 / p>

import pandas as pd

import numpy as np

from tkinter import *

from tkinter.ttk import *

from tkinter import messagebox

import threading

import Test1

import Test2

# In[ ]:



def Load1():



    Test1.func()

    messagebox.showinfo( "Successful","Reconcilation Complete")



def Load2():

    Test2.func()

    try:

        messagebox.showinfo( "Successful","Reconcilation Complete")

    except Exception as inst:

        messagebox.showinfo( "Unsuccessful",inst)

root = Tk()



root.geometry('375x100')

root.title("Reco")



root.configure(background="LightBlue2")

style = Style()

style.configure('TButton', background = 'SeaGreen2', font =

               ('calibri', 20, 'bold'))



btn1 = Button(root, text = 'Tier Recon', command =threading.Thread(target=Load1).start )

btn1.grid(row = 1, column = 3, pady = 10, padx = 100)



btn2 = Button(root, text = 'View Recon', command =threading.Thread(target=Load2).start)

btn2.grid(row = 2, column = 3, pady = 10, padx = 100)



root.mainloop()

1 个答案:

答案 0 :(得分:1)

我假设您的Test1Test2函数是无限的,所以我将其创建为Test1.pyTest2.py的格式:

# Test1.py / Test2.py
import tkinter as tk
def func():
    while True:
        root = tk.Tk()
        root.mainloop()

现在,对于您的代码,我将强烈建议您放弃当前的格式,而转而使用Object Oriented Programming,因为这将在将来为您省去许多麻烦!

这是我为使其工作而编写的代码:

import tkinter as tk
import tkinter.ttk as ttk
import Test1
import Test2

class reco_win:
    def __init__(self, master):
        self.master = master
        self.master.geometry('375x100')
        self.master.configure(background="LightBlue2")
        style = ttk.Style()
        style.configure('TButton', background = 'SeaGreen2', font =
                ('calibri', 20, 'bold'))

        btn1 = ttk.Button(self.master, text = 'Tier Recon', command = lambda: self.master.after(1, self.load1))

        btn1.grid(row = 1, column = 3, pady = 10, padx = 100)

        btn2 = ttk.Button(self.master, text = 'View Recon', command =lambda: self.master.after(1, self.load1))

        btn2.grid(row = 2, column = 3, pady = 10, padx = 100)

    def load1(self):
        Test1.func()

    def load2(self):
        Test2.func()

def main():
    root = tk.Tk()
    reco_win(root)
    root.mainloop()

if __name__ == '__main__':
    main()

此代码中的重要位:

self.master.after(1, self.load1)

这行代码在1毫秒后执行的操作将异步启动新线程并执行功能Test1.func()

这意味着您不必担心在python中管理多线程模块的问题,而可以编写更多代码!

希望这会有所帮助,

詹姆斯

P.S。

如果您在tk.Tk()中使用Test1/Test2.py窗口,则可以改用tk.TopLevel窗口,这样您可以将这段代码重写为:

import tkinter as tk
import tkinter.ttk as ttk
import Test1
import Test2

class reco_win:
    def __init__(self, master):
        self.master = master
        self.master.geometry('375x100')
        self.master.configure(background="LightBlue2")
        style = ttk.Style()
        style.configure('TButton', background = 'SeaGreen2', font =
                ('calibri', 20, 'bold'))

        btn1 = ttk.Button(self.master, text = 'Tier Recon', command = self.load1)

        btn1.grid(row = 1, column = 3, pady = 10, padx = 100)

        btn2 = ttk.Button(self.master, text = 'View Recon', command =self.load2)

        btn2.grid(row = 2, column = 3, pady = 10, padx = 100)

    def load1(self):
        top = tk.Toplevel()
        tk.Label(top, text="Hello").grid(row=0, column=0)

    def load2(self):
        top2 = tk.Toplevel()
        tk.Label(top2, text="Hello 2").grid(row=0, column=0)

def main():
    root = tk.Tk()
    reco_win(root)
    root.mainloop()

if __name__ == '__main__':
    main()