将 tkinter 进度条链接到功能 [英] linking tkinter progress bar to function
问题描述
所以我有一个数学程序,我让用户输入 10 个问题.十个问题结束后,它会显示您答对了多少等.
so i have a maths program and i have the user entering 10 questions. once the ten questions are over it displays how many you got correct etc.
我想要做的是将进度条链接到已回答的问题数量,因此如果用户已完成 5 个问题,则进度将进行一半,然后一旦完成 10 个问题,则重新开始.我目前在这里有这个定义用于提交答案
what i want to do is link the progress bar to the amount of questions answered so if user has done 5 questions progress will be half way then once theve done 10 start all over again. i currently have this definition here for submitting the answers
def submit(self):
try:
user_answer = int(self.answer_strvar.get())
except:
return
if eval(self.equation) == user_answer:
print('Correct!! The Answer Was {}'.format(user_answer))
self.correct_counter += 1
else:
print('Wrong!! Your Answer was: {} = {}, The Correct answer is {}'.format(self.equation, user_answer, eval(self.equation)))
self.submit_counter += 1
if self.submit_counter < NUM_QUESTIONS:
self.update_equation()
else:
self.show_result()
self.submit_counter = 0
self.correct_counter = 0
其中提交计数器是用户提交的答案数量.这是我想要将其链接到的变量,该数字是完成的百分比,10 是最大值.
where as submit counter is the amount of answers submitted by user. that is the variable i want to link it to with that number being the percentage done and 10 being the maxximum.
我在主屏幕上也有一个这样的进度条
i also have a progress bar like this on the main screen
pb = ttk.Progressbar(self, orient="horizontal", length=600, mode="determinate")
pb.pack()
推荐答案
使用控制变量来设置值.
Use a control variable to set the value.
class TestProgress():
def __init__(self):
self.root = tk.Tk()
self.root.title('ttk.Progressbar')
self.val=tk.IntVar()
self.val.set(0)
self.pbar = ttk.Progressbar(self.root, length=300,
maximum=10, variable=self.val)
self.pbar.pack(padx=5, pady=5)
tk.Label(self.root, textvariable=self.val,
bg="lightblue").pack()
## wait 2 seconds & update
self.root.after(2000, self.advance)
self.root.mainloop()
def advance(self):
self.val.set(8)
TP=TestProgress()
这篇关于将 tkinter 进度条链接到功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!