【问题标题】:tkinter set a maximum number of button clickstkinter 设置最大按钮点击次数
【发布时间】:2014-11-15 07:18:33
【问题描述】:

我正在为我的 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 之前就这样做了,但我还不能让它工作。

【问题讨论】:

    标签: python loops tkinter


    【解决方案1】:

    您不需要 while 循环。只需保留一个计数器变量,每次尝试后递增。当变量超过某个阈值时,停止游戏。

    【讨论】:

    • 我已经想到了,但是我应该把 counter+=1 放在哪里呢? (为了让它在每次按下按钮时递增)我会定义一个名为 counter 的全新函数并向按钮添加第二个命令(如果可以的话?)?
    • @Trey:放入guess
    【解决方案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()
    

    【讨论】:

    • 啊当然我为什么没有想到这一点。谢谢!
    猜你喜欢
    • 2014-12-06
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 2015-05-02
    • 1970-01-01
    • 2014-05-08
    相关资源
    最近更新 更多