Python tkinter 标签在函数开始时不会改变
我使用 tkinter 和 Python 为将 Excel 文件转换为 CSV 的程序创建用户界面。
我创建了一个标签作为状态栏,并将 statusBarText 设置为 StringVar() 作为文本变量。 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!')
当您单击转换按钮时,将调用此函数,并且一切正常,除了状态栏永远不会更改为“正在转换...”。
如果输入或输出为空,状态栏文本将更改为无效参数,并且根据返回码更改为成功或失败。 问题是它永远不会更改为“正在转换...”
我已将该确切的行复制并粘贴到 if 语句中并且它工作正常,但由于某种原因它在子进程运行之前永远不会改变,当它位于子进程的顶部时功能。 任何帮助将不胜感激。
I'm using tkinter with Python to create a user interface for a program that converts Excel files to CSV.
I created a label to act as a status bar, and set statusBarText as a StringVar() as the textvariable. inputFileEntry and outputFileEntry are textvariables that contain the input and output file paths.
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. Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您在单个方法调用中完成所有这些操作,因此在启动子流程之前 GUI 永远没有机会更新。 查看 update_idletasks() 调用...
来自 http://infohost.nmt .edu/tcc/help/pubs/tkinter/universal.html
w.update_idletasks()
更新显示的一些任务(例如调整大小和重绘小部件)被称为空闲任务,因为它们通常被推迟到应用程序完成事件处理并返回主循环等待新事件为止。
如果您想在应用程序下一次空闲之前强制更新显示,请在任何小部件上调用 w.update_idletasks() 方法。
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. Check out update_idletasks() call...
from 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.
If you want to force the display to be updated before the application next idles, call the w.update_idletasks() method on any widget.
您如何创建您的标签?
我有这个小测试设置:
它有效。
您是否确保使用“textvariable = StatusBarText”而不是“text=StatusBarText.get()”?
How are you creating your Label?
I have this little test setup:
And it works.
Did you make sure to use "textvariable = StatusBarText" instead of "text=StatusBarText.get()"?