简体   繁体   中英

How can i make my tkinter window autoupdate itself?

I'm trying to make myself a small game where the user is given a task and then has to do it. I'm using a stack (told to as it's a school project) and i add to the stack. After adding to it, I want the most recent item to be peeked so they know what task to do and also the money counter to change. My code can be seen here:

`

import tkinter as tk
import time as time
import random as random

stack = []
money = 0

def push(item):
    global stack
    stack.append(item)


def pop():
    global stack
    stack_length = len(stack)
    if stack_length == 0:
        pass
    else:
        stack.pop()

def peek():
    global stack
    if len(stack) == 0:
        pass
    else:
        return stack[-1]



def random_cars():
    global stack
    global tasks
    tasks = ["wash", "vac", "polish", "dry", "interior detail", "exterior detail", "full detail", "wheel wash"]
    random_number = random.randint(0,7)
    task_to_add = tasks[random_number]
    if len(stack) <= 10:
        push(task_to_add)
        window.update()
        
    

def job():
    global tasks
    correct_answers = tasks
    correct_answers[0] = "exterior"
    correct_answers[1] = "interior"
    correct_answers[2] = "exterior"
    correct_answers[3] = "exterior"
    correct_answers[4] = "interior"
    correct_answers[5] = "exterior"
    correct_answers[6] = "both"
    correct_answers[7] = "exterior"
    return correct_answers


def start():
    random_cars()
    start_button.config(state="disabled")
    exterior_button.config(state="normal")
    interior_button.config(state="normal")
    both_button.config(state="normal")
    task_to_do_is = peek()
    global stack
    
    if len(stack) == 0:
        info_text.set("There are no jobs yet.")
        random_cars()
       
            
    else:
        info_text.set("The task you need to do is: " + task_to_do_is)


def exterior():
    global tasks
    global stack
    global money
    button_click = "exterior"
    task_doing = peek()
    correct_anwers = job()
    for i in range(len(tasks)):
        if tasks[i] == task_doing:
            if correct_anwers[i] == button_click:
                pop()
                money += 10


def interior():
    pass

def both():
    pass

window = tk.Tk()
window.title("Car wash simulator")

title = tk.Label(window, text="Car wash simulator", font=("Arial", 30))
title.grid(column=0, row=0, padx=75)

info_text = tk.StringVar()
info_text.set("The aim of the game is to select the correct task. \n You will be given a job in a random order, \n and you have to decide whether it is an interior or exterior job.")

game_info = tk.Label(window, textvariable=info_text, font=("Arial", 15))
game_info.grid(column=0, row=1, padx=75)

start_button = tk.Button(window, text="Start game", command=start)
start_button.grid(column=0, row=2, padx=75)

frame = tk.Frame(window)
frame.grid(column=0, row=3)

exterior_button = tk.Button(frame, text="exterior", command=exterior)
exterior_button.grid(row=0, column=0)
exterior_button.config(state="disabled")

interior_button = tk.Button(frame, text="interior", command=interior)
interior_button.grid(row=0, column=2)
interior_button.config(state="disabled")

both_button = tk.Button(frame, text="both", command=both)
both_button.grid(row=0, column=1)
both_button.config(state="disabled")

money_label_text = tk.StringVar()
money_label_text.set("Money:" + str(money))
money_label = tk.Label(window, textvariable=money_label_text, font=("Ariel", 15))
money_label.grid(column=0, row=4, padx=75)


window.mainloop()


`

I've tried using window.update() and window.after(1000, update) but neither have seemed to work at all.

Try this

def job():
    # global tasks # No need to use global here..
    # correct_answers = tasks  # Here You assign list to correct_answers but still correct_answers points to tasks list.
    correct_answers = tasks.copy()   # So you copy to copy the list. 
    # Or.
    # correct_answers = tasks[:]
    ...


And for updating money.

def exterior():
    ...
            if correct_anwers[i] == button_click:
                pop()
                money += 10
                # Add this line
                money_label_text.set("Money: " + str(money))

Also Your code uses some Not needed global


Full Updated Code

import tkinter as tk
import time as time
import random as random

stack = []
money = 0

def push(item):
    global stack
    stack.append(item)


def pop():
    global stack
    stack_length = len(stack)
    if stack_length == 0:
        pass
    else:
        stack.pop()

def peek():
    global stack
    if len(stack) == 0:
        pass
    else:
        return stack[-1]



def random_cars():
    global stack
    global tasks
    tasks = ["wash", "vac", "polish", "dry", "interior detail", "exterior detail", "full detail", "wheel wash"]
    random_number = random.randint(0,7)
    task_to_add = tasks[random_number]
    if len(stack) <= 10:
        push(task_to_add)
        window.update()
        
    

def job():
    global tasks
    correct_answers = tasks.copy()
    correct_answers[0] = "exterior"
    correct_answers[1] = "interior"
    correct_answers[2] = "exterior"
    correct_answers[3] = "exterior"
    correct_answers[4] = "interior"
    correct_answers[5] = "exterior"
    correct_answers[6] = "both"
    correct_answers[7] = "exterior"
    return correct_answers


def start():
    random_cars()
    start_button.config(state="disabled")
    exterior_button.config(state="normal")
    interior_button.config(state="normal")
    both_button.config(state="normal")
    task_to_do_is = peek()
    global stack
    
    if len(stack) == 0:
        info_text.set("There are no jobs yet.")
        random_cars()
       
            
    else:
        info_text.set("The task you need to do is: " + task_to_do_is)


def exterior():
    global tasks
    global stack
    global money
    button_click = "exterior"
    task_doing = peek()
    correct_anwers = job()
    for i in range(len(tasks)):
        if tasks[i] == task_doing:
            if correct_anwers[i] == button_click:
                pop()
                money += 10
                money_label_text.set("Money: " + str(money))


def interior():
    pass

def both():
    pass

window = tk.Tk()
window.title("Car wash simulator")

title = tk.Label(window, text="Car wash simulator", font=("Arial", 30))
title.grid(column=0, row=0, padx=75)

info_text = tk.StringVar()
info_text.set("The aim of the game is to select the correct task. \n You will be given a job in a random order, \n and you have to decide whether it is an interior or exterior job.")

game_info = tk.Label(window, textvariable=info_text, font=("Arial", 15))
game_info.grid(column=0, row=1, padx=75)

start_button = tk.Button(window, text="Start game", command=start)
start_button.grid(column=0, row=2, padx=75)

frame = tk.Frame(window)
frame.grid(column=0, row=3)

exterior_button = tk.Button(frame, text="exterior", command=exterior)
exterior_button.grid(row=0, column=0)
exterior_button.config(state="disabled")

interior_button = tk.Button(frame, text="interior", command=interior)
interior_button.grid(row=0, column=2)
interior_button.config(state="disabled")

both_button = tk.Button(frame, text="both", command=both)
both_button.grid(row=0, column=1)
both_button.config(state="disabled")

money_label_text = tk.StringVar()
money_label_text.set("Money:" + str(money))
money_label = tk.Label(window, textvariable=money_label_text, font=("Ariel", 15))
money_label.grid(column=0, row=4, padx=75)


window.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM