[Python] Get input in Python tkinter Entry when Button pressed

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a 'guess the number' game with Pyhon tkinter but so far I have not been able to retrieve the input from the user.

    How can I get the input in entry when b1 is pressed?

    I also want to display a lower or higher message as a clue to the player but I am not sure if what I have is right:

    import time
    import random
    import decimal
    import tkinter as tk

    root = tk.Tk()

    randomnum = float(decimal.Decimal(random.randrange(100,10000))/100)
    guess = 0

    def get(entry):
    guess = entry.get()
    return guess

    def main():
    b1 = tk.Button(root, text="Guess", command=get)
    entry = tk.Entry()
    b1.grid(column=1, row=0)
    entry.grid(column=0, row=0)
    root.mainloop()
    print(guess)
    if guess < randomnum:
    l2 = tk.Label(root, text="Higher!")
    l2.grid(column=0, row=2)
    elif guess > randomnum:
    l3 = tk.Label(root, text="Lower!")
    l3.grid(column=0, row=2)


    while guess != randomnum:
    main()
    l4 = tk.Label(root, text="Well guessed")
    time.sleep(10)

    Continue reading...

Compartilhe esta Página