如何阻止我的特金特窗口冻结?



下面是我用来运行两个python文件的主要函数,但是一旦我点击了buttuon,我的窗口就会冻结。请告诉我一种执行多次脱粒的方法,以便我可以同时单击两个按钮。

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()

我假设你的Test1Test2函数有点无限,所以我创建了这个作为我的Test1.pyTest2.py的格式:

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

现在有了你的代码,我强烈建议你摆脱当前的格式,转向面向对象编程,因为它会为你节省很多麻烦!

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

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中管理多线程模块的问题,而是可以编写更多代码!

希望这有帮助,

詹姆斯

附言

如果您在Test1/Test2.py中使用tk.Tk()窗口,则可以改用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()

相关内容

  • 没有找到相关文章

最新更新