从 Python Tkinter 中主要 class 之外的函数更新进度条
Update progress bar from function outside the main class in Python Tkinter
我已经初始化了一个进度条,想根据 Tkinter class 之外的函数/线程更新它。我已经尝试了此处提供的所有与问题类似的解决方案,但无济于事。任何帮助将不胜感激。这是我的代码:
from tkinter import ttk
import time
import tkinter as tk
def run():
global progressBar
progressBar['maximum'] = 100
for i in range(0,100,25):
time.sleep(0.05)
progressBar["value"] = i
progressBar.update()
progressBar["value"] = 0
progressBar["value"] = 100
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
buttonFrame = tk.Label(text="Progress Bar")
buttonFrame.grid(column=0,row=0)
progressBar = ttk.Progressbar(self, orient="horizontal", length=286,mode="determinate")
progressBar.grid(column = 0, row = 3, pady=10)
button1 = tk.Button(buttonFrame, text="Run Progress Bar" ,command = run)
button1.grid(column = 0, row = 0)
app = Main()
app.mainloop()
你用过OOP.You可以充分利用class.I重构你的代码(另外,你的代码有错误):
from tkinter import ttk
import time
import tkinter as tk
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.buttonFrame = tk.Label(text="Progress Bar")
self.buttonFrame.grid(column=0,row=0)
self.progressBar = ttk.Progressbar(self, orient="horizontal", length=286,mode="determinate")
self.progressBar.grid(column = 0, row = 3, pady=10)
# this shouldn't be run() or self.run().No "()" there(Or you can use lambda if you need to pass arguments).
self.button1 = tk.Button(self.buttonFrame, text="Run Progress Bar" ,command = self.run)
self.button1.grid(column = 0, row = 0)
def run(self):
self.progressBar['maximum'] = 100
for i in range(0, 100, 25):
time.sleep(0.05)
self.progressBar["value"] = i
self.progressBar.update()
self.progressBar["value"] = 0
self.progressBar["value"] = 100
app = Main()
app.mainloop()
嗨,我怎么把这个发到 运行?
from tkinter import ttk
import time
import tkinter as tk
class Main(tk.Tk):
def __init__(self, *args,**kwargs):
tk.Tk.__init__(self,*args, **kwargs)
self.buttonFrame = tk.Label(text="Progress Bar")
self.buttonFrame.grid(column=0,row=0)
self.progressBar = ttk.Progressbar(self, orient="horizontal", length=286,mode="determinate")
self.progressBar.grid(column = 0, row = 3, pady=10)
# this shouldn't be run() or self.run().No "()" there(Or you can use lambda if you need to pass arguments).
self.button1 = tk.Button(self.buttonFrame, text="Run Progress Bar" ,command = self.run)
self.button1.grid(column = 0, row = 0)
def run(self, data):
self.progressBar['maximum'] = 100
for i in range(data):
time.sleep(0.05)
self.progressBar["value"] = i
self.progressBar.update()
self.progressBar["value"] = 0
self.progressBar["value"] = 100
class second():
def pass_it(self):
data = 99
Main().run(data)
我已经初始化了一个进度条,想根据 Tkinter class 之外的函数/线程更新它。我已经尝试了此处提供的所有与问题类似的解决方案,但无济于事。任何帮助将不胜感激。这是我的代码:
from tkinter import ttk
import time
import tkinter as tk
def run():
global progressBar
progressBar['maximum'] = 100
for i in range(0,100,25):
time.sleep(0.05)
progressBar["value"] = i
progressBar.update()
progressBar["value"] = 0
progressBar["value"] = 100
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
buttonFrame = tk.Label(text="Progress Bar")
buttonFrame.grid(column=0,row=0)
progressBar = ttk.Progressbar(self, orient="horizontal", length=286,mode="determinate")
progressBar.grid(column = 0, row = 3, pady=10)
button1 = tk.Button(buttonFrame, text="Run Progress Bar" ,command = run)
button1.grid(column = 0, row = 0)
app = Main()
app.mainloop()
你用过OOP.You可以充分利用class.I重构你的代码(另外,你的代码有错误):
from tkinter import ttk
import time
import tkinter as tk
class Main(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.buttonFrame = tk.Label(text="Progress Bar")
self.buttonFrame.grid(column=0,row=0)
self.progressBar = ttk.Progressbar(self, orient="horizontal", length=286,mode="determinate")
self.progressBar.grid(column = 0, row = 3, pady=10)
# this shouldn't be run() or self.run().No "()" there(Or you can use lambda if you need to pass arguments).
self.button1 = tk.Button(self.buttonFrame, text="Run Progress Bar" ,command = self.run)
self.button1.grid(column = 0, row = 0)
def run(self):
self.progressBar['maximum'] = 100
for i in range(0, 100, 25):
time.sleep(0.05)
self.progressBar["value"] = i
self.progressBar.update()
self.progressBar["value"] = 0
self.progressBar["value"] = 100
app = Main()
app.mainloop()
嗨,我怎么把这个发到 运行?
from tkinter import ttk
import time
import tkinter as tk
class Main(tk.Tk):
def __init__(self, *args,**kwargs):
tk.Tk.__init__(self,*args, **kwargs)
self.buttonFrame = tk.Label(text="Progress Bar")
self.buttonFrame.grid(column=0,row=0)
self.progressBar = ttk.Progressbar(self, orient="horizontal", length=286,mode="determinate")
self.progressBar.grid(column = 0, row = 3, pady=10)
# this shouldn't be run() or self.run().No "()" there(Or you can use lambda if you need to pass arguments).
self.button1 = tk.Button(self.buttonFrame, text="Run Progress Bar" ,command = self.run)
self.button1.grid(column = 0, row = 0)
def run(self, data):
self.progressBar['maximum'] = 100
for i in range(data):
time.sleep(0.05)
self.progressBar["value"] = i
self.progressBar.update()
self.progressBar["value"] = 0
self.progressBar["value"] = 100
class second():
def pass_it(self):
data = 99
Main().run(data)