【发布时间】:2023-01-05 23:41:12
【问题描述】:
有人可以帮我解决 python 中的线程问题并让进度条正常工作吗?
即使研究给出了很多结果,我也无法让它发挥作用。
我以前从未做过线程,我不知道把东西放在哪里正确。
出于测试目的,我准备了一个带有按钮和进度条的简单 GUI:
单击按钮后,将弹出一个简单的 3d 图。
这样的绘图可能需要一些计算时间,而用户需要等待,我希望 GUI 不被冻结并且进度条动画化。
目前 GUI 冻结,直到绘图显示出来。然后进度条开始动画。
我已经阅读了很多关于线程并将计算和 gui 放到不同的线程? 但我只是不想让它工作。 有人能够向我解释更多,指导我解决类似的问题或文档吗? 或者,万一很快就解决了,超越简单的代码并按应有的方式更正它?
在此先感谢您提供任何帮助。
到目前为止的 Python 脚本:
from time import sleep
from tkinter import EW
import ttkbootstrap as ttk
import numpy as np
import matplotlib.pyplot as plt
def main():
def plot_sample():
sleep(5) # simulate calculation times
x = np.outer(np.linspace(-2, 2, 30), np.ones(30))
y = x.copy().T # transpose
z = np.cos(x ** 2 + y ** 2)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(x, y, z,cmap='viridis', edgecolor='none')
ax.set_title('Surface plot')
plt.show()
def progressbar_start():
progressbar.grid(row=1, column=0, sticky=EW, padx=10, pady=10) # let progressbar appear in GUI
progressbar.start(interval=10)
def progressbar_stop():
progressbar.stop()
progressbar.grid_forget() # hide progressbar when not needed
def button_clicked():
progressbar_start() # start progressbar before computation begins
plot_sample() # plotting
progressbar_stop() # stop progressbar after plot is done
# GUI
# window size and settings
root = ttk.Window()
# Basic settings for the main window
root.title('Test progressbar')
root.minsize(300, 200)
root.resizable(True, True)
root.configure(bg='white')
# grid settings for the main window in which LabelFrames are sitting in
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
root.rowconfigure(1, weight=1)
# Button fto show 3d-plot
button_calc_3dplot = ttk.Button(root, text='Calculate 3D Plot', command=button_clicked)
button_calc_3dplot.grid(row=0, column=0, padx=5, pady=5)
progressbar = ttk.Progressbar(style='success-striped', mode='indeterminate')
# end of GUI
root.mainloop()
if __name__ == "__main__":
main()
【问题讨论】:
标签: python python-3.x tkinter