【发布时间】:2022-12-02 07:49:15
【问题描述】:
Say that we have an entry_object, a button button_object and a global variable called score.
I want to update the score when the button is clicked and the entry has a value. I tried looking at this answer but I need something slightly different. The main difference is the storing of the value in a variable.
I have a function to generate the elements:
def generate_gui():
root = tk.Tk()
root.geometry('900x700+50+50')
entry_object = tk.Entry(root, width=40)
entry_object.pack()
button_object = tk.Button(root, text='submit')
button_object.pack()
return root, entry_object, button_object
I now want to update score with `entry_object.get()' when the button is clicked.
generate_gui()
while accumulated_score < 10:
for player in players:
** #### pseudocode: if button_object clicked then player.accumulated_score += score#### **
How do I replace the pseudocode with the right code? Please help :)
【问题讨论】:
-
Research for tk.StringVar or IntVar and use the option of text variable. In addition, for GUI code you usually don't use a while loop, since the event loop is driven.
-
You mention several player objects but don't have any player objects in the code you posted. Also, you don't have any sort of widget for displaying a score.