【问题标题】:Tkinter while loop guess gameTkinter while循环猜谜游戏
【发布时间】:2022-01-27 23:39:38
【问题描述】:
# Tkinter guessing game
from tkinter import *
import random


window = Tk()

# Bot Generator
bot = random.randint(1, 20+1)
print(bot)


def submit():
    tries = 5
    while tries >= 0:
        e1 = (int(guessE.get()))
        if e1 == bot:
            print("You win")
            break
            
        elif e1 != bot:
            print("Try again")
            tries -= 1
            break


def clear():
    guessE.delete(0, "end")
    
    
# Guess Label
guessL = Label(window, text="Guess a number (between 1 and 20}")
guessL.place(x=75, y=50)

# Guess Entry
guessE = Entry(window, width=25, bg="lightgrey")
guessE.place(x=95, y= 80)

# Submit Button
submitBtn = Button(window, width=10, height=2, text="Submit", command=submit)
submitBtn.place(x=85, y=115)

# Clear Button
clearBtn = Button(window, width=10, height=2, text="Clear", command=clear)
clearBtn.place(x=175, y=115)


window.geometry("350x350")
window.resizable(0, 0)
window.attributes("-topmost", True)
window.mainloop()

我正在尝试使用 Tkinter 创建一个猜谜游戏,我已经创建了所有条目和标签,清除按钮正在工作。我正在努力创建一个while循环,一旦使用了5次尝试,我需要程序停止接受提交的条目。关于如何解决这个问题的任何想法?

【问题讨论】:

  • 为什么你认为需要while循环?
  • 而不是在您的submit 函数中使用while 循环,您应该只减少一个全局(或类成员)tries 变量。在 GUI 应用程序中运行这样的循环不是一个好习惯,因为它通过占用所有必须运行的唯一线程来冻结它。我还会考虑将用户反馈打印到 GUI 元素(随便什么标签)而不是使用打印。
  • @acw1668 老实说,我实际上并不确定,我正在学习 Tkinter,我认为 while 循环对于 Tk 上的猜谜游戏来说是一个很好的方法。
  • 感谢@PaulRooney 的帮助,有道理(:

标签: python tkinter random while-loop


【解决方案1】:

就像@Paul Rooney 已经提到的那样,我也认为 while 循环是一个糟糕的设计。但是,这是一个带有全局变量和一些小改动的工作示例(当达到 0 次尝试时禁用条目和按钮):

# Tkinter guessing game
from tkinter import *
import random


window = Tk()

# Bot Generator
bot = random.randint(1, 20+1)
print(bot)

# define outside of function to not overwrite each time
tries = 5


def submit():
    global tries  # to make tries a global variable
    while tries > 0:  # this matches your tries amount correctly
        e1 = (int(guessE.get()))
        if e1 == bot:
            print("You win")
            break
            
        elif e1 != bot:
            print("Try again")
            tries -= 1
            break

    # print status, disable the widgets
    if tries == 0:
        print("No more tries left")
        guessE.configure(state="disabled")
        submitBtn.configure(state="disabled")


def clear():
    guessE.delete(0, "end")
    
    
# Guess Label
guessL = Label(window, text="Guess a number (between 1 and 20}")
guessL.place(x=75, y=50)

# Guess Entry
guessE = Entry(window, width=25, bg="lightgrey")
guessE.place(x=95, y= 80)

# Submit Button
submitBtn = Button(window, width=10, height=2, text="Submit", command=submit)
submitBtn.place(x=85, y=115)

# Clear Button
clearBtn = Button(window, width=10, height=2, text="Clear", command=clear)
clearBtn.place(x=175, y=115)


window.geometry("350x350")
window.resizable(0, 0)
window.attributes("-topmost", True)
window.mainloop()

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 2023-03-15
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多