如何制作一个与主程序并排运行的Tkinter加载屏幕



我正在尝试使用Tkinter创建一个小型python应用程序,该应用程序涉及在多个列表中收集数据,如下所示:

def main_process():
list1 = []
list2 = []
list3 = []

#Data collection process
#Resulting lists ready

数据收集过程大约需要10秒,所以我想制作一个";加载屏幕";它在数据收集过程中并行工作,不需要一个接一个。对于状态栏,我考虑引入一个值从0开始的变量n,并在每个结果列表准备就绪时增加其值。

我试图创建一个函数loading_screen(n(,该函数将在数据处理之前调用,其中n是前面提到的包含数值的变量。当数据处理发生时,我将运行loading_screen函数来引入n

def main_process():

def loading_screen(n):
root = Tk()
root.title('Stock Market App')
root.geometry('800x300')

status_label = Label(root, text = n)
status_label.pack()
loading_screen(0)

# list1 Ready
n += 10
root.after(0, lambda: loading_screen(n))
# list2 ready
n += 10
root.after(0, lambda: loading_screen(n))
# And so on...

但在所有数据处理完成后,它最终向我显示了加载屏幕。有人能帮我吗?

谢谢。

将函数放在函数中不是一个好主意,最好创建一个class

然后创建两个单独的Tk窗口。

此外,after能够传递参数,因此lambda不是必需的。

为了符合你的问题,我做了一些小调整。窗户现在将并排放置。

这就是你想要的吗?

import tkinter as tk
class main_process:
def loading_screen( self, n ):
self.status_label[ 'text' ] = '{}'.format( n )
self.root.deiconify()
self.status_label.focus()
self.root.update()
# insert processing here
def __init__( self, x ):
self.root = tk.Tk()
self.root.withdraw()
self.root.title('Stock Market App')
self.root.geometry( f'700x300+{x}+240')
self.status_label = tk.Label( self.root, font = 'Helvetica 20 normal')
self.status_label.pack()
self.root.update()
screen_1 = main_process( 0 )
screen_1.root.after( 10, screen_1.loading_screen, 1 )
screen_2 = main_process( 705 )
screen_2.root.after( 10, screen_2.loading_screen, 2 )
screen_2.root.mainloop()

嗨,你可以像这个一样删除gui

Labelone.destroy()
.
.
.
Loading_screen.place(...)
#you data collection code
Loading_screen.destroy()
Labelone.place(...)
.
.
.

因此,您的数据收集将在后台进行,并加载gui,以便用户在python收集数据时看到加载

最新更新