0
import tkinter as tk
import random

correct_num = random.randint(1,100)
print(correct_num)

def result(num):
    global correct_num,lb
    print(num)
    if num == correct_num:
        lb.config(text='You did it! Congraulations!',fg='blue')
    else:
        if num < correct_num:
            lb.config(text='Guess higher',fg='red')
        else:
            lb.config(text='Guess lower',fg='red')
        
win = tk.Tk()
win.title('Guess number')
win.geometry('500x400')

for i in range(10):
    for j in range(1,11):
        number = i*10+j
        button = tk.Button(win,text=number,command=lambda:result(number))
        button.grid(row=i,column=j,padx=10)



lb = tk.Label(win,text='')
lb.grid(row=11,column=5,columnspan=2)

For this program, I do not have any errors but when I run, then whatever I pressed any buttons and try to print(num) it shows 100. I would like to put the num corresponding to the button number that I pressed. May I ask is there any mistakes that I have not amended in order to make it work successfully?

Dom807
  • 27
  • 7

0 Answers0