Even if you get it right, the message doesn’t change!

Asked

Viewed 77 times

0

I’m trying to create a game in Tkinter, it’s simple thing, you kick a number and then a message appears if your kick is right or wrong. I already did this in CLI, however, I went to try to make a GUI for this game and even if you hit the kick, still appears an error message. Please help me out.

# Just some tkinter
import tkinter as tk
from tkinter import messagebox
from random import randint as rd

class screen:
    def __init__(self, master):
        self.master = master

        self.frame1 = tk.Frame(self.master)
        self.frame2 = tk.Frame(self.master)
        self.frame1.pack()
        self.frame2.pack(side=tk.BOTTOM)

        self.lbl = tk.Label(self.frame1, text='Test your luck!\nPick a number!')
        self.lbl.pack()

        self.txt = tk.Entry(self.frame1)
        self.txt.get()
        self.txt.pack(side=tk.BOTTOM)

        self.btn = tk.Button(self.frame1, text='Send', command=self.random)
        self.btn.pack(side=tk.BOTTOM)

        self.lbl2 = tk.Label(self.frame2, text='')

    def random(txt):
        random_num = rd(1, 10)
        if txt == random_num:
            messagebox.showinfo('Congratulations!', 'Correct number!')
        else:
            messagebox.showerror('Error', f'Wrong number.\nCorrect number = {random_num}.')



if __name__ == '__main__':
    root = tk.Tk()
    root.title('The Lucky Game!')
    screen(root)
    root.mainloop()
  • The txt within the method random is the very screen, then you have to take the value of the field, something like if int(txt.txt.get()) == random_num: etc...

  • @hkotsubo, I can create a solution with this answer?

1 answer

0

In the method random you need to pass the class instance (an object) Screen with the self being that it represents its graphical interface, and through the self we can access the property txt which is the field and access the value by the method get() and make the comparison as follows:

def random(self):
    random_num = rd(1, 10)
    if int(self.txt.get()) == random_num:
        messagebox.showinfo("Congratulations!", "Correct number!")
    else:
        messagebox.showerror("Error", f"Wrong number.\nCorrect number = {random_num}.")

See below the behavior you expect from your application:

tela do app para adivinhar numero

Application code:

# Just some tkinter
import tkinter as tk
from tkinter import messagebox
from random import randint as rd


class Screen:
    def __init__(self, master):
        self.master = master

        self.frame1 = tk.Frame(self.master)
        self.frame2 = tk.Frame(self.master)
        self.frame1.pack()
        self.frame2.pack(side=tk.BOTTOM)

        self.lbl = tk.Label(self.frame1, text='Test your luck!\nPick a number!')
        self.lbl.pack()

        self.txt = tk.Entry(self.frame1)
        self.txt.get()
        self.txt.pack(side=tk.BOTTOM)

        self.btn = tk.Button(self.frame1, text='Send', command=self.random)
        self.btn.pack(side=tk.BOTTOM)

        self.lbl2 = tk.Label(self.frame2, text='')

    def random(self):
        random_num = rd(1, 10)
        if int(self.txt.get()) == random_num:
            messagebox.showinfo('Congratulations!', 'Correct number!')
        else:
            messagebox.showerror('Error', f'Wrong number.\nCorrect number = {random_num}.')


if __name__ == '__main__':
    root = tk.Tk()
    root.title('The Lucky Game!')
    Screen(root)
    root.mainloop()

The answer of the user @Danizavtz not this wrong and points to the same path that is to make the conversion of the function return get() which is a string for an integer type and perform the comparison.

Read to documentation.

  • It worked! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.