我正在 python 3.11.3 中使用 tkinter 库,我将在应用程序中放入一些音乐,但在此之前我验证了按钮和标签出现,但我认为我在逻辑上存在问题,当数字应该正确时,'不出现“正确”标签。 对不起我的英语(语言非常糟糕) 它的代码:
import tkinter as tk
from pygame import mixer
import random
class guessingnumberapp(tk.Tk):
def __init__(self):
super().__init__()
self.title("guessingnumberapp")
self.geometry("800x600")
self.iconbitmap("iconito.ico")
self.welcome_page()
def welcome_page(self):
self.configure(bg="aqua")
tk.Label(self,text = "the guessin number game",bg= "aqua",fg="black",font = ("Modern",30)).pack(pady = 10)
tk.Button(self,text = "play",bg ="blue3",fg ="white",font =("Georgia",25),command=self.game_page).pack(pady= 50)
def game_page(self):
for widget in self.winfo_children():
widget.destroy()
self.configure(bg="chartreuse1")
enter = tk.Label(self,text= "Enter any number",bg ="#3D9140",fg="white",font = ("Comic Sans MS",12))
enter.place(x = 10,y = 80)
self.entrada = tk.Entry(self,font = ("Rockwell",12))
self.entrada.config(bg = "#FFF8DC",fg ="black")
self.entrada.pack
self.entrada.place(x = 150 ,y = 80)
tk.Button(self,text= "check",bg ="#3D9140",fg="white",font = ("Comic Sans MS",12),command= self.check).place(x= 500,y=80)
def check(self):
wawa = self.entrada.get()
guess = int(wawa)
self.calculating(guess)
def calculating(self, guess):
n = random.randrange(1,10)
while n != guess:
if guess < n:
palabras = tk.Label(self, text ="too low", bg = "chartreuse1", fg = "white",font=("Calibri", 20))
palabras.place(x=160, y= 250)
palabras.after(7000,palabras.destroy)
self.entrada = tk.Entry(self,font = ("Rockwell",12))
self.entrada.config(bg = "#FFF8DC",fg ="black")
self.entrada.pack
self.entrada.place(x = 150 ,y = 80)
wawa = self.entrada.get()
guess =int(wawa)
elif guess > n:
palabras = tk.Label(self, text ="too high", bg = "chartreuse1", fg = "white",font=("Calibri", 20))
palabras.place(x=160, y= 250)
palabras.after(7000,palabras.destroy)
self.entrada = tk.Entry(self,font = ("Rockwell",12))
self.entrada.config(bg = "#FFF8DC",fg ="black")
self.entrada.pack
self.entrada.place(x = 150 ,y = 80)
wawa = self.entrada.get()
guess =int(wawa)
else:
break
palabras = tk.Label(self, text= "correct", bg = "chartreuse1", fg = "white",font=("Calibri", 20))
palabras.place(x=160, y= 250)
palabras.after(7000,palabras.destroy)
app = guessingnumberapp()
app.mainloop()
当我输入 2 时,结果非常低,当我输入 4 时,结果非常高,当我输入 3 时,结果非常低,而不是正确的。
代码中的问题是每次调用
calculating()
函数时都会生成不同的数字。要解决您的问题,您需要移动线路
n = random.randrange(1, 10)
在
calculating()
函数之外。这是固定代码:
import tkinter as tk
from pygame import mixer
import random
class guessingnumberapp(tk.Tk):
def __init__(self):
super().__init__()
self.title("guessingnumberapp")
self.geometry("800x600")
self.iconbitmap("iconito.ico")
self.welcome_page()
def welcome_page(self):
self.configure(bg="aqua")
tk.Label(self,text = "the guessin number game",bg= "aqua",fg="black",font = ("Modern",30)).pack(pady = 10)
tk.Button(self,text = "play",bg ="blue3",fg ="white",font =("Georgia",25),command=self.game_page).pack(pady= 50)
def game_page(self):
for widget in self.winfo_children():
widget.destroy()
self.configure(bg="chartreuse1")
enter = tk.Label(self,text= "Enter any number",bg ="#3D9140",fg="white",font = ("Comic Sans MS",12))
enter.place(x = 10,y = 80)
self.entrada = tk.Entry(self,font = ("Rockwell",12))
self.entrada.config(bg = "#FFF8DC",fg ="black")
self.entrada.pack
self.entrada.place(x = 150 ,y = 80)
tk.Button(self,text= "check",bg ="#3D9140",fg="white",font = ("Comic Sans MS",12),command= self.check).place(x= 500,y=80)
def check(self):
wawa = self.entrada.get()
guess = int(wawa)
self.calculating(guess)
# Generate a random number
n = random.randrange(1, 10)
print("Correct number: " + str(n)) # For debugging purposes; you can remove this line
def calculating(self, guess):
if guess < self.n:
palabras = tk.Label(self, text ="too low", bg = "chartreuse1", fg = "white",font=("Calibri", 20))
palabras.place(x=160, y= 250)
palabras.after(7000,palabras.destroy)
elif guess > self.n:
palabras = tk.Label(self, text ="too high", bg = "chartreuse1", fg = "white",font=("Calibri", 20))
palabras.place(x=160, y= 250)
palabras.after(7000,palabras.destroy)
elif guess == self.n:
palabras = tk.Label(self, text= "correct", bg = "chartreuse1", fg = "white",font=("Calibri", 20))
palabras.place(x=160, y= 250)
palabras.after(7000,palabras.destroy)
app = guessingnumberapp()
app.mainloop()