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 methodrandom
is the veryscreen
, then you have to take the value of the field, something likeif int(txt.txt.get()) == random_num: etc...
– hkotsubo
@hkotsubo, I can create a solution with this answer?
– Danizavtz