Sam*_*Sam 5 python tk-toolkit tkinter splash-screen multiprocessing
我已经实现了一个启动画面,当我的应用程序在启动时从远程云存储加载数据库时显示。启动画面通过调用 .update() 保持活动状态(上面有一个进度条),并在单独的加载过程结束后销毁。在此之后,主循环启动并且应用程序正常运行。
下面的代码曾经在我的 Mac 上使用 python 3.6 和 tcl/tk 8.5.9 运行良好。但是,在更新到 Sierra 之后,我被迫将 tk 更新到 ActiveTcl 8.5.18。现在,在单独的进程完成之前不会显示启动画面,但随后会与根窗口一起出现并停留在屏幕上(即使调用了它的 .destroy() 方法)。
import tkinter as tk
import tkinter.ttk as ttk
import multiprocessing
import time
class SplashScreen(tk.Toplevel):
def __init__(self, root):
tk.Toplevel.__init__(self, root)
self.geometry('375x375')
self.overrideredirect(True)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.label = ttk.Label(self, text='My Splashscreen', anchor='center')
self.label.grid(column=0, row=0, sticky='nswe')
self.center_splash_screen()
print('initialized splash')
def center_splash_screen(self):
w = self.winfo_screenwidth()
h = self.winfo_screenheight()
x = w / 2 - 375 / 2
y = h / 2 - 375 / 2
self.geometry("%dx%d+%d+%d" % ((375, 375) + (x, y)))
def destroy_splash_screen(self):
self.destroy()
print('destroyed splash')
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.start_up_app()
self.title("MyApp")
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.application_frame = ttk.Label(self, text='Rest of my app here', anchor='center')
self.application_frame.grid(column=0, row=0, sticky='nswe')
self.mainloop()
def start_up_app(self):
self.show_splash_screen()
# load db in separate process
process_startup = multiprocessing.Process(target=App.startup_process)
process_startup.start()
while process_startup.is_alive():
# print('updating')
self.splash.update()
self.remove_splash_screen()
def show_splash_screen(self):
self.withdraw()
self.splash = SplashScreen(self)
@staticmethod
def startup_process():
# simulate delay while implementation is loading db
time.sleep(5)
def remove_splash_screen(self):
self.splash.destroy_splash_screen()
del self.splash
self.deiconify()
if __name__ == '__main__':
App()
Run Code Online (Sandbox Code Playgroud)
我不明白为什么会发生这种情况以及如何解决它。有人可以帮忙吗?谢谢!
如果您注释掉该行,启动画面将正确显示self.overrideredirect(True)。但是,我不想要窗口装饰,它在脚本结束时仍然保留在屏幕上。虽然它在内部被销毁,任何进一步的方法调用self.splash(例如.winfo_...-methods)都会导致_tkinter.TclError: bad window path name ".!splashscreen".
此外,此代码在 windows 和 tcl/tk 8.6 下运行良好。这是 Mac 上 tcl/tk 8.5.18 窗口管理的错误/问题吗?
显然,这是由于调用 后窗口管理器未装饰窗口时窗口堆叠顺序出现问题overrideredirect(True)。其他平台上似乎也出现过这种情况。
在使用 Python 3.6.1 和 tcl/tk 8.5.18 的 macOS 10.12.5 上运行以下代码,单击“打开”按钮后不会出现顶层窗口:
import tkinter as tk
class TL(tk.Toplevel):
def __init__(self):
tk.Toplevel.__init__(self)
self.overrideredirect(True)
# self.after_idle(self.lift)
tl_label = tk.Label(self, text='this is a undecorated\ntoplevel window')
tl_label.grid(row=0)
b_close = tk.Button(self, text='close', command=self.close)
b_close.grid(row=1)
def close(self):
self.destroy()
def open():
TL()
root = tk.Tk()
label = tk.Label(root, text='This is the root')
label.grid(row=0)
b_open = tk.Button(root, text='open', command=open)
b_open.grid(row=1)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
取消注释该行self.after_idle(self.lift)可以解决问题(简单地调用self.lift()也可以解决问题)。但是使用after_idle()可以防止窗口在移动到其位置并调整大小之前闪烁一小会儿,这是我在 tkinter 中反复遇到的另一个问题,让我想知道我是否应该继续学习 PyQT 还是 PySide2...)。
至于在我原来的问题中关闭未装饰的窗口的问题:调用 after_idle(window.destroy()) 而不是 window.destroy() 似乎也可以解决这个问题。我不懂为什么。
如果其他人重现此问题,并且有人提示我在哪里将其报告为错误,我很乐意这样做。
| 归档时间: |
|
| 查看次数: |
1579 次 |
| 最近记录: |