【问题标题】:How to fix my Function so it works in my Tkinter Application (Guessing Game)如何修复我的功能,使其在我的 Tkinter 应用程序中工作(猜谜游戏)
【发布时间】:2026-02-19 23:30:01
【问题描述】:

基本上,我想检查答案并每次使用按钮检查是否正确,然后将其从列表中弹出。在另一个文件/代码/应用程序中,当我尝试在我的 Gui 应用程序上使用它时它正在工作,它不会迭代,只会保持第一个答案正确。

我尝试了多种方法,例如使用字典和获取密钥,但在按下按钮后它仍然没有迭代。

这是可以正常工作的代码:

while answers == True:
answers = ["Sayajin", "Namek", "Cell", "TournamentOfPower"]
for a in  range(4):

    s = input()
    if s in answers[0]:
        print("Richtig")
        answers.pop(0)

这是不起作用的代码:

def check(event):
answers = ["Sayajin", "Namek", "Cell", "TournamentOfPower"]
for a in range(4):

    s = entrysaga.get()
    if s in answers[0]:
        print("Richtig")
        answers.pop(0)

完整代码:

from tkinter import *
import tkinter.messagebox
import time
import random

root = Tk()

#Hint Button
hint = Button(root, text= "Hint")
hint.bind("<Button-1>")
hint.place(x=50, y=20)

#How to Play Button + Info Message how to play

def Howtoplay():
    tkinter.messagebox.showinfo("How to play", "To start the game u have 
to press the button (Start)\n--------------------------------------------- 
----------------\n"
                                           ""
                                           "Then the Picture will switch 
and its going to show u a Character and u have to guess from which Dragon 
Ball Saga he is.\n-------------------------------------------------------- 
-----\n"
                                           "Just type it in the Entry and 
press Check after that if u were right the next picture shows up")

info = Button(root, text="How to Play", command=Howtoplay)
info.bind("<Button-1>")
info.place(x=150, y=20)

#textwidget
textwidget = Label(root, text="Entry the DragonBall Saga:")

#entry widget
entrysaga = Entry(root)

#Pictures for guessing the Saga
Sayajin = PhotoImage(file="sayajinsaga.png")
Namek = PhotoImage(file="NamekSaga.png")
Cell = PhotoImage(file="CellSaga.png")
Buu = PhotoImage(file="BuuSaga.png")
TournamentOfPower = PhotoImage(file="TournamentOfPowersaga.png")

#Start function
def start():
    labelSagas.config(image=Sayajin)


#define check for pictures
def check(event):
    answers = ["Sayajin", "Namek", "Cell", "TournamentOfPower"]
    for a in range(4):

        s = entrysaga.get()
        if s in answers[0]:
            print("Richtig")
            answers.pop(0)

#button check
buttonsaga = Button(root, text="Check")
buttonsaga.bind("<Button-1>", check)

textwidget.place(x=300, y=170)
entrysaga.place(x=300, y= 200)
buttonsaga.place(x=440, y=195)

#Start Button
start = Button(root, text="Start", command=start)
start.bind("<Button-1")
start.place(x=400, y=20)

# Label with start picture,
startpic = PhotoImage(file="dbzsagas.png")
labelSagas = Label(root, image=startpic)
labelSagas.place(x=25, y=80)



#size of window
root.geometry("500x280")

#window title
root.title("Dragon Ball Saga´s guessing game")

#start of the window
root.mainloop()

我的例外输出应该像它迭代的第一个代码中一样,并且在得到第一个答案之后就可以进入下一个。但实际结果呢,还是停留在第一位。

【问题讨论】:

  • 请问您可以发布您的完整代码吗?知道entrysaga 来自哪里会很有用。运行此程序时是否遇到任何错误?
  • 您好,我已经添加了完整的代码,不,我没有收到任何错误,程序可以运行,但只是没有迭代答案。

标签: python list button tkinter


【解决方案1】:

我已经移动了当您按下Start 按钮时应该出现的小部件。它们都在def start(): 函数中,否则它们不会出现。

由于entrysaga 在不同的函数中被调用,它无法找到输入的值,因为该变量不是全局变量。使用global entrysaga 意味着它可以并且可以从脚本中的任何位置调用。

看起来是这样的:

from tkinter import *
import tkinter.messagebox
import time
import random

root = Tk()

#Hint Button
hint = Button(root, text= "Hint")
hint.bind("<Button-1>")
hint.place(x=50, y=20)

#How to Play Button + Info Message how to play

def Howtoplay():
    tkinter.messagebox.showinfo("How to play", "To start the game u have to press the button (Start)\n--------------------------------------------- ----------------\n"
                                           ""
                                           "Then the Picture will switch and its going to show u a Character and u have to guess from which Dragon Ball Saga he is.\n-------------------------------------------------------- -----\n"
                                           "Just type it in the Entry and press Check after that if u were right the next picture shows up")

info = Button(root, text="How to Play", command=Howtoplay)
info.bind("<Button-1>")
info.place(x=150, y=20)

#Pictures for guessing the Saga
Sayajin = PhotoImage(file="sayajinsaga.png")
Namek = PhotoImage(file="NamekSaga.png")
Cell = PhotoImage(file="CellSaga.png")
Buu = PhotoImage(file="BuuSaga.png")
TournamentOfPower = PhotoImage(file="TournamentOfPowersaga.png")

#Start function
def start():
    labelSagas.config(image=Sayajin)
    global entrysaga
    entrysaga = tkinter.Entry(root)
    entrysaga.place(x=300, y= 200)
    buttonsaga = Button(root, text="Check")
    buttonsaga.bind("<Button-1>", check)
    buttonsaga.place(x=440, y=195)
    textwidget = Label(root, text="Entry the DragonBall Saga:")
    textwidget.place(x=300, y=170)
#define check for pictures
def check(event):
    answers = ["Sayajin", "Namek", "Cell", "TournamentOfPower"]
    for a in range(4):

        s = entrysaga.get()
        if s in answers[0]:
            print("Richtig")
            answers.pop(0)

#Start Button
start = Button(root, text="Start", command=start)
start.bind("<Button-1")
start.place(x=400, y=20)

# Label with start picture,
startpic = PhotoImage(file="dbzsagas.png")
labelSagas = Label(root, image=startpic)
labelSagas.place(x=25, y=80)

#size of window
root.geometry("500x280")

#window title
root.title("Dragon Ball Saga´s guessing game")

#start of the window
root.mainloop()

希望这会有所帮助! :)

【讨论】:

  • 好吧,我已经尝试过了,它仍然可以正常工作。我还在 answers.pop(0) print(answers) 之后添加,它在控制台中显示它被弹出但是当我尝试写“Namek”并再次检查它仍然不起作用它不调用“Richtig”。唯一的事情即使我弹出功能只是重做它,它仍然是“Sayajin”。我是否可能必须在另一个函数中执行此操作,并且基本上将答案附加到另一个变量中的某个位置,然后尝试检查它?
  • 只有“Sayajin”有效的原因是因为if s in answers[0]:,如果您删除[0],那么该列表中的任何内容都会调用“Richtig”(即使不是正确答案)。您需要做的是调用图像,然后将条目与图像匹配,因此只有当图像和entrysaga.get()匹配时它才会通过。或者,您可以更改[0] 以匹配列表中的答案,因此如果图像是“Namek”,您可以将[0] 更改为[1]。这可以在if 语句中实现。
最近更新 更多