ttk.Progressbar 使用 python 3.1.4 tkinter 8.5 无法启动
我已经编写了一些 python 3.1 代码(非常简单,因为我不是程序员),并且我正在尝试使用 tkinter 8.5 和 python 3.1.4 在其中放入 GUI。我遇到的问题是进度条无法启动。这是代码的一部分:
def transformation(Xn,Yn,Zn,const):
infile = filedialog.askopenfile('r')
outfile = filedialog.asksaveasfile('w')
pbar.start()
for line in infile.readlines():
inlist = line.split(" ")
inlist = [float(i) for i in inlist]
l = (Xn+Yn+Zn)/const**2
Xm = inlist[0] + Xn*l
Ym = inlist[1] + Yn*l
Zm = inlist[2] + Zn*l
outlist=[0,0,0]
outlist[0] = inlist[0] + 2*(Xm-inlist[0])
outlist[1] = inlist[1] + 2*(Ym-inlist[1])
outlist[2] = inlist[2] + 2*(Zm-inlist[2])
outdata = str('%.4f' %outlist[0])+" "+str('%.4f' %outlist[1])+" "+str('%.4f' %outlist[2])+"\n"
outfile.writelines(outdata)
infile.close()
outfile.close()
pbar.stop()
该函数由按钮调用。我提供了程序运行所需的所有文件。计算已成功完成,但条形图从未开始。有什么想法吗?
谢谢, 亚历克斯·Th
I've written some python 3.1 code (really simple as im no programmer) and I'm trying to put a GUI in it using tkinter 8.5 and python 3.1.4. The problem I'm having is that the progress bar won't start. Here's the part of the code:
def transformation(Xn,Yn,Zn,const):
infile = filedialog.askopenfile('r')
outfile = filedialog.asksaveasfile('w')
pbar.start()
for line in infile.readlines():
inlist = line.split(" ")
inlist = [float(i) for i in inlist]
l = (Xn+Yn+Zn)/const**2
Xm = inlist[0] + Xn*l
Ym = inlist[1] + Yn*l
Zm = inlist[2] + Zn*l
outlist=[0,0,0]
outlist[0] = inlist[0] + 2*(Xm-inlist[0])
outlist[1] = inlist[1] + 2*(Ym-inlist[1])
outlist[2] = inlist[2] + 2*(Zm-inlist[2])
outdata = str('%.4f' %outlist[0])+" "+str('%.4f' %outlist[1])+" "+str('%.4f' %outlist[2])+"\n"
outfile.writelines(outdata)
infile.close()
outfile.close()
pbar.stop()
The function is being called by a button. I give all the files needed for the programm to work. The calculation is successfully completed but the bar never starts. Any ideas?
Thanks,
AlexTh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
栏正在启动,您只是看不到它,因为您没有给 UI 重绘的机会。当屏幕重新绘制时,进度条已停止。重绘是为了响应重绘事件而发生的,并且这些事件由事件循环处理。当您的代码处于读取数据的循环中时,您正在阻止事件循环运行。
您需要a)使用线程或单独的进程来执行IO,以免事件循环挨饿,b)您需要将处理分解为可以在事件循环的每次迭代期间完成的小块,或 c) 在循环的每次迭代期间调用 update_idletasks ;此方法处理“空闲”事件,其中包括屏幕刷新。
谷歌搜索“tkinter 长时间运行计算”以获得很多建议。
The bar is starting, you're just not seeing it because you don't give the UI a chance to redraw. By the time the screen redraws you have stopped the progress bar. Redraws happen in response to redraw events, and those events get processed by the event loop. While your code is in the loop that reads the data you are preventing the event loop from running.
You will need to either a) use a thread or separate process to do your IO so as not to starve the event loop, b) you need to break your processing up into small chunks that can be done during each iteration of the event loop, or c) call
update_idletasks
during each iteration of your loop; this method processes "idle" events which includes screen refreshes.Google for "tkinter long running calculation" for lots of advice.
您需要使用步骤或设置更新进度条,或者更改附加到进度条的 IntVar 变量的值。
这可能会有所帮助。本例启动一个主窗口,可以打开多个进度条
通过自动方法在循环中递增。在这个小例子中,您的截止日期循环服务于自动方法的目的。
将 tkinter 导入为 tk
从 tkinter 导入 ttk
导入时间
class main_window:
class pbar_dialog:
if name == 'main':
You need to update the progress bar with step or set, or change the value of the IntVar variable that is attached to the progress bar.
This might help. This example starts a main window, which can open multiple progress bars
that are incremented in a loop via the automatic method. Your deadlines loop serves the purpose of the automatic method in this little example.
import tkinter as tk
from tkinter import ttk
import time
class main_window:
class pbar_dialog:
if name == 'main':