简体   繁体   English

Python tkinter标签在功能开始时不会改变

[英]Python tkinter label won't change at beginning of function

I'm using tkinter with Python to create a user interface for a program that converts Excel files to CSV. 我正在使用tkinter和Python为将Excel文件转换为CSV的程序创建用户界面。

I created a label to act as a status bar, and set statusBarText as a StringVar() as the textvariable. 我创建了一个标签作为状态栏,并将statusBarText设置为StringVar()作为textvariable。 inputFileEntry and outputFileEntry are textvariables that contain the input and output file paths. inputFileEntry和outputFileEntry是包含输入和输出文件路径的文本变量。

def convertButtonClick():
    statusBarText.set('Converting...')

    if inputFileEntry.get() == '' or outputFileEntry.get() == '':
        statusBarText.set('Invalid Parameters.')
        return

    retcode = subprocess.('Program.exe' ,shell=true)

    if retcode == 0:
        statusBarText.set('Conversion Successful!')
    else:
        statusBarText.set('Conversion Failed!')

This function gets called when you click the convert button, and everything is working fine EXCEPT that the status bar never changes to say 'Converting...'. 单击转换按钮时会调用此函数,一切正常,但状态栏永远不会更改为“转换...”。

The status bar text will get changed to invalid parameters if either the input or output are empty, and it will change to success or failure depending on the return code. 如果输入或输出为空,状态栏文本将更改为无效参数,并且将根据返回代码更改为成功或失败。 The problem is it never changes to 'Converting...' 问题是它永远不会改变为'转换......'

I've copied and pasted that exact line into the if statements and it works fine, but for some reason it just never changes before the subprocess runs when its at the top of the function. 我已经将那条确切的行复制并粘贴到if语句中,并且它工作正常,但由于某种原因,它只是在子进程运行之前永远不会更改它位于函数顶部。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Since you're doing all of this in a single method call, the GUI never gets a chance to update before you start your sub process. 由于您在单个方法调用中执行所有这些操作,因此在启动子进程之前,GUI永远不会有机会更新。 Check out update_idletasks() call... 查看update_idletasks()调用...

from http://infohost.nmt.edu/tcc/help/pubs/tkinter/universal.html 来自http://infohost.nmt.edu/tcc/help/pubs/tkinter/universal.html

w.update_idletasks() Some tasks in updating the display, such as resizing and redrawing widgets, are called idle tasks because they are usually deferred until the application has finished handling events and has gone back to the main loop to wait for new events. w.update_idletasks()更新显示的一些任务w.update_idletasks()例如调整大小和重绘小部件)被称为空闲任务,因为它们通常被推迟到应用程序完成处理事件并返回主循环以等待新事件。
If you want to force the display to be updated before the application next idles, call the w.update_idletasks() method on any widget. 如果要在应用程序下次空闲之前强制更新显示,请在任何窗口小部件上调用w.update_idletasks()方法。

How are you creating your Label? 你是如何创建标签的? I have this little test setup: 我有这个小测试设置:

from Tkinter import *
class LabelTest:

    def __init__(self, master):
        self.test = StringVar()

        self.button = Button(master, text="Change Label", command=self.change)
        self.button.grid(row=0, column=0, sticky=W)

        self.test.set("spam")
        self.testlabel = Label(master, textvariable = self.test).grid(row = 0,column = 1)
    def change(self):

        self.test.set("eggs")



root = Tk()
root.title("Label tester")
calc = LabelTest(root)

root.mainloop()

And it works. 它有效。 Did you make sure to use "textvariable = StatusBarText" instead of "text=StatusBarText.get()"? 你确定使用“textvariable = StatusBarText”而不是“text = StatusBarText.get()”吗?

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

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