重启随机数猜谜游戏
restart random number guessing game
到目前为止,我已经在 python 中创建了一个猜随机数的游戏。它工作正常,但我想创建一个重置按钮,允许用户再次猜测但与第一次不同的数字。到目前为止,这是我的代码
import tkinter as tk
import random
randombroj=random.randint(0, 10)
def check():
result=int(glvnes.get()) #the value of entry widget
if result==randombroj:
gldugme.grid_forget() #hide check button widget
glvnes.grid_forget() #hide entry widget
bravo=tk.Label(root, text="Congratulations!!") #congratulations widget
bravo.config(width=40)
bravo.grid(pady=0, row=1)
def restart(): #restart function located in check function
randombroj=random.randint(0, 10) #get new random number
bravo.destroy() #destroy congratulations widget
weset.destroy() #destroy restart button
gldugme.grid(pady=20, row=2) #display again check button
glvnes.grid(pady=0, row=1) #display again entry widget
weset=tk.Button(root, text="Restart", font=("arial italic", 18), bg='#6BDF63', command=restart) #the restart button
weset.config(width=13, heigh=2)
weset.grid(pady=20, row=2)
root=tk.Tk()
root.title("Guess The Secret Number")
root.geometry('450x250')
root.config(bg="#F2E06B")
glmeni=tk.Label(root, text="Can you gess the secret number?", bg='#8CCEC0', font=("arial italic", 19)) #main widget
glmeni.config(heigh=2)
glmeni.grid(pady=30, padx=28, row=0)
glvnes=tk.Entry(root, text="Enter number here:") #entry box
glvnes.config(width=40)
glvnes.grid(pady=0, row=1)
gldugme=tk.Button(root, text="Check", font=("arial italic", 18),
bg='#6BDF63', command=check) #check button
gldugme.config(width=13, heigh=2)
gldugme.grid(pady=20, row=2)
root.mainloop()
当你按下复选按钮时,如果它是正确的,它会显示祝贺,然后复选按钮被替换为重置按钮。当我点击它时,条目小部件和复选按钮重新打开,但随机值没有改变。它保持第一次的样子。我该如何解决?
您需要从条目中删除最后的猜测,并在 restart()
:
中声明 randomobroj
为全局
def restart(): #restart function located in check function
global randombroj
glvnes.delete(0, "end") # clear last guess
...
到目前为止,我已经在 python 中创建了一个猜随机数的游戏。它工作正常,但我想创建一个重置按钮,允许用户再次猜测但与第一次不同的数字。到目前为止,这是我的代码
import tkinter as tk
import random
randombroj=random.randint(0, 10)
def check():
result=int(glvnes.get()) #the value of entry widget
if result==randombroj:
gldugme.grid_forget() #hide check button widget
glvnes.grid_forget() #hide entry widget
bravo=tk.Label(root, text="Congratulations!!") #congratulations widget
bravo.config(width=40)
bravo.grid(pady=0, row=1)
def restart(): #restart function located in check function
randombroj=random.randint(0, 10) #get new random number
bravo.destroy() #destroy congratulations widget
weset.destroy() #destroy restart button
gldugme.grid(pady=20, row=2) #display again check button
glvnes.grid(pady=0, row=1) #display again entry widget
weset=tk.Button(root, text="Restart", font=("arial italic", 18), bg='#6BDF63', command=restart) #the restart button
weset.config(width=13, heigh=2)
weset.grid(pady=20, row=2)
root=tk.Tk()
root.title("Guess The Secret Number")
root.geometry('450x250')
root.config(bg="#F2E06B")
glmeni=tk.Label(root, text="Can you gess the secret number?", bg='#8CCEC0', font=("arial italic", 19)) #main widget
glmeni.config(heigh=2)
glmeni.grid(pady=30, padx=28, row=0)
glvnes=tk.Entry(root, text="Enter number here:") #entry box
glvnes.config(width=40)
glvnes.grid(pady=0, row=1)
gldugme=tk.Button(root, text="Check", font=("arial italic", 18),
bg='#6BDF63', command=check) #check button
gldugme.config(width=13, heigh=2)
gldugme.grid(pady=20, row=2)
root.mainloop()
当你按下复选按钮时,如果它是正确的,它会显示祝贺,然后复选按钮被替换为重置按钮。当我点击它时,条目小部件和复选按钮重新打开,但随机值没有改变。它保持第一次的样子。我该如何解决?
您需要从条目中删除最后的猜测,并在 restart()
:
randomobroj
为全局
def restart(): #restart function located in check function
global randombroj
glvnes.delete(0, "end") # clear last guess
...