When I create a couple of definitions in between each other, it sometimes doesn't see the import
s, such as massege box, destroy from tkinter, and sleep command from time. It's been a week since I started learning python and I don't know how to fix it.
Error for the destroy()
command :
File "C:\Users\enes baba\Desktop\animemusic\test.py", line 58, in roll
destroy()
NameError: name 'destroy' is not defined
Press any key to continue . . .
Error for the sleep()
command :
File C:\Users\enes baba\Desktop\animemusic\test.py", line 77, in <lambda>
Button(page_play, text="Roll The Dice", command=lambda: roll()).place(x=50, y=100)
File "C:\Users\enes baba\Desktop\animemusic\test.py", line 56, in roll
sleep(5)
NameError: name 'sleep' is not defined
The code:
def open21():
global game
global player_num
global computer_num
if pass_code != 1:
messagebox.showerror("Login First", "You need to LOGIN first to be able to play.")
if pass_code == 1:
page_play = Toplevel()
page_play.title("Play21")
page_play.geometry("400x400")
Label(page_play, text=player_num).place(x=20, y=10)
Label(page_play, text=computer_num).place(x=90, y=10)
def roll():
import tkinter
from tkinter import messagebox
global player_num
global computer_num
player_num = random.randint(1, 10)
Label(page_play, text=player_num).place(x=20, y=10)
if player_num > 21:
player_num = random.randint(1, 10)
computer_num = random.randint(1, 10)
messagebox.showerror("Lost", "You've exceeded 21. You've Lost The Game. Try Again")
sleep(5)
# game = 2
destroy(page_play)
def pas():
global player_num
global computer_num
while computer_num < 13:
computer_num = random.randint(1, 10)
while (21 - player_num) < (21 - computer_num):
computer_num = random.randint(1, 10)
Label(page_play, text=computer_num).place(x=90, y=10)
# sleep(3)
if computer_num > 21:
messagebox.showinfo("Won", "Congrats the computers number have exceeded 21. You won")
destroy(page_play)
else:
messagebox.showinfo("Won", "Congrats Your number is closer to 21. You won")
# sleep(2)
destroy(page_play)
Button(page_play, text="Roll The Dice", command=lambda: roll()).place(x=50, y=100)
Button(page_play, text="Pass", command=lambda: pas()).place(x=150, y=100)
CodePudding user response:
Instead of destroy(page_play)
try
page_play.destroy()
destroy
is a method so you have to call it this way.
To get the sleep function to work, add
from time import sleep