linking tkinter progress bar to function

我的梦境 提交于 2019-12-13 06:48:02

问题


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.

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

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()

回答1:


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()


来源:https://stackoverflow.com/questions/36417319/linking-tkinter-progress-bar-to-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!