我正在为我的python类使用tkinter制作一个简单的猜谜游戏,并且想知道是否有办法循环它以便玩家在程序告诉玩家该号码是什么之前会有最大数量的猜测。更改数字,或在程序告诉他们答案后终止程序。到目前为止,我的代码是:
# This program is a number guessing game using tkinter gui.
# Import all the necessary libraries.
import tkinter
import tkinter.messagebox
import random
# Set the variables.
number = random.randint(1,80)
attempts = 0
# Start coding the GUI
class numbergameGUI:
def __init__(self):
# Create the main window.
self.main_window = tkinter.Tk()
# Create four frames to group widgets.
self.top_frame = tkinter.Frame()
self.mid_frame1 = tkinter.Frame()
self.mid_frame2 = tkinter.Frame()
self.bottom_frame = tkinter.Frame()
# Create the widget for the top frame.
self.top_label = tkinter.Label(self.top_frame, \
text='The number guessing game!')
# Pack the widget for the top frame.
self.top_label.pack(side='left')
# Create the widgets for the upper middle frame
self.prompt_label = tkinter.Label(self.mid_frame1, \
text='Guess the number I\'m thinking of:')
self.guess_entry = tkinter.Entry(self.mid_frame1, \
width=10)
# Pack the widgets for the upper middle frame.
self.prompt_label.pack(side='left')
self.guess_entry.pack(side='left')
# Create the widget for the bottom middle frame.
self.descr_label = tkinter.Label(self.mid_frame2, \
text='Your Guess is:')
self.value = tkinter.StringVar()
# This tells user if guess was too high or low.
self.guess_label = tkinter.Label(self.mid_frame2, \
textvariable=self.value)
# Pack the middle frame's widgets.
self.descr_label.pack(side='left')
self.guess_label.pack(side='left')
# Create the button widgets for the bottom frame.
self.guess_button = tkinter.Button(self.bottom_frame, \
text='Guess', \
command=self.guess,)
self.quit_button = tkinter.Button(self.bottom_frame, \
text='Quit', \
command=self.main_window.destroy)
# Pack the buttons.
self.guess_button.pack(side='left')
self.quit_button.pack(side='left')
# Pack the frames
self.top_frame.pack()
self.mid_frame1.pack()
self.mid_frame2.pack()
self.bottom_frame.pack()
# Enter the tkinter main loop.
tkinter.mainloop()
# Define guess
def guess(self):
# Get the number they guessed.
guess1 = int(self.guess_entry.get())
# sattempts +=1
# Tell player too low if their guess was too low.
if guess1 < number:
self.value.set('too low')
# Tell player too high if their guess was too high.
elif guess1 > number:
self.value.set('too high')
# End the loop if the player attempts the correct number.
if guess1 == number:
tkinter.messagebox.showinfo('Result', 'Congratulations! You guessed right!')
start = numbergameGUI()
我试着在guess函数中加入一个while循环,因为我在程序使用tkinter之前就这样做了,但是我还没能让它工作。
答案 0 :(得分:2)
你不需要一个while循环。只需保留一个计数器变量,在每次尝试后递增。当变量超过某个阈值时,停止游戏。
答案 1 :(得分:2)
像
这样的东西def guess(self):
self.num_guesses += 1
# Get the number they guessed.
guess1 = int(self.guess_entry.get())
# sattempts +=1
# Tell player too low if their guess was too low.
if guess1 < number:
self.value.set('too low')
# Tell player too high if their guess was too high.
elif guess1 > number:
self.value.set('too high')
# End the loop if the player attempts the correct number.
if guess1 == number:
tkinter.messagebox.showinfo('Result', 'Congratulations! You guessed right!')
self.main_window.quit()
elif self.num_guesses >= self.max_guesses:
tkinter.messagebox.showinfo('Bye Bye', "That's all the guesses you get")
self.main_window.quit()