简体   繁体   English

如何在不等待 tkinter Menu 命令功能完成的情况下更新 tkinter 标签文本?

[英]How do I get tkinter label text to update without waiting for the tkinter Menu command function to finish?

I'm writing a practice GUI application that helps manage android apps by adb.我正在编写一个练习 GUI 应用程序,帮助通过 adb 管理 android 应用程序。

But I've ran into an issue were the status bar won't update until the uninstall_package function, that is called from tkinter menu, has finished.但是我遇到了一个问题,直到从 tkinter 菜单调用的uninstall_package函数完成后,状态栏才会更新。 The first self.status_bar.text.set change isn't seen even if I halt the function with time.wait() instead of terminal function.即使我使用time.wait()而不是终端函数停止函数,也看不到第一个self.status_bar.text.set更改。

I came across this related issue but it hasn't helped resolve it.我遇到了这个相关的问题,但它没有帮助解决它。 https://stackoverflow.com/a/32504779/6318164 https://stackoverflow.com/a/32504779/6318164

This is the script.这是脚本。 I've excluded some parts to minimise for this post to show only the main part of the issue.我已经排除了一些部分以最小化这篇文章以仅显示问题的主要部分。

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry('640x480')

        self.menu_bar = MenuBar(self)
        self.status_bar = StatusBar(self)
        self.tab = Tab(self)

    def uninstall_package(self):
        if self.tab.get_selected_name() == 'Third Party':
            # com.company.app
            package = self.tab.third_party.get_selected_items()[0]

            answer = messagebox.askyesno(
                title='Confirm',
                message='Uninstall package:\n{}'.format(package)
            )

            if not answer:
                return

            self.status_bar.text.set('Uninstalling {}...'.format(package))

            # function halts until the terminal process has finished
            process = terminal('adb uninstall {}'.format(package))
        
            if process.stdout:
                self.status_bar.text.set('Success')
            else:
                messagebox.showerror('An error occurred.')

            # other functions...

class MenuBar(tk.Menu):
    def __init__(self, master):
        super().__init__(master)

        action_menu = tk.Menu(self, tearoff=0)
        action_menu.add_command(label='Uninstall Selected Package', command=master.uninstall_package)

        self.add_cascade(label='Action', menu=action_menu)

        master.config(menu=self)

class StatusBar(tk.Frame):
    def __init__(self, master):
        super().__init__(master)

        self.text = tk.StringVar()
        self.text.set('Ready')

        label = tk.Label(self, anchor='w', bd=1, relief=tk.SUNKEN, textvariable=self.text)
        label.pack(fill='x')

        self.pack(side='bottom', fill='x')

The first self.status_bar.text.set isn't shown but the last one is.第一个self.status_bar.text.set未显示,但最后一个显示。 The terminal终点站

您是否在设置状态栏文本后尝试过?

tk.update_idletasks()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM