0

基本上,我想检查答案并每次使用按钮检查是否正确,然后将其从列表中弹出。在另一个文件/代码/应用程序中,当我尝试在我的 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()

我的例外输出应该像在第一个代码中一样,它迭代并在获得第一个答案之后直接进入下一个。但实际结果它停留在第一个。

4

1 回答 1

0

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()

希望这可以帮助!:)

于 2019-01-21T17:21:46.137 回答