當我在彼此之間創建幾個定義時,它有時看不到imports,例如 massege box、destroy from tkinter 和 sleep 命令。我開始學習python已經一周了,我不知道如何解決它。
destroy()命令錯誤:
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 . . .
sleep()命令錯誤:
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
編碼:
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)
uj5u.com熱心網友回復:
而不是destroy(page_play)嘗試
page_play.destroy()
destroy 是一種方法,因此您必須以這種方式呼叫它。
要使睡眠功能正常作業,請添加
from time import sleep
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/365158.html
上一篇:如何將在Tkinter文本框中輸入的值放入Python中的函式引數中?
下一篇:Tkinter條目未更新
