from tkinter import *
def corequiz():
Guess.get()
global Guess_num
Guess_num = Guess_num + 1
if song == Guess:
print("congratulations the song was " + song)
if Guess_num == 1:
print("well done " + name + " you guessed it on your first try")
User_score = User_score + 3
print(name + " your points are " + str(User_score))
elif Guess_num == 2:
print("well " + name + " you did it on your second try")
User_score = User_score + 1
else:
print("you failed to answer the question")
print("your current points are " + str(User_score))
else:
print("wrong")
def Quiz(name,User_score,artist,song):
root = Tk()
root.title("game")
root.geometry("720x500+0+0")
heading = Label(root, text="welcome to the quiz game",font=("arial",24,"bold"),fg = "black").pack()
tip = Label(root, text=("this song is by " + artist + " and the first few letters of the song is: " + song[0] + song[1]),font=("italics",10,"normal"),fg = "black").place(x=10,y=50)
usernameenter = heading = Label(root, text="enter your guess of the song: ",font = ("arial",11,"bold"),fg="black").place(x=10,y=100)
Guess = StringVar()
entry_box2 = Entry(root, textvariable= Guess,width=80,bg="light blue").place(x=222,y=105)
entryguess = Button(root, text="Submit",width=30, height=3,bg="lightblue",command=corequiz).place(x=250,y=200)
root.mainloop()
#theses are example variables
global Guess_num
Guess_num = 0
global song
song = "shake it off"
global artist
artist = "Taylor swift"
global User_score
User_score = 1
global name
name = "gamer"
Quiz(name,User_score,artist,song)
我是tkinter的新人。
我无法获得此代码来使Guess传递给corequiz子例程。
当您单击猜测子程序的子例程时,我如何才能从输入框中获取guess.get()来获取变量。
我尝试过创建全局变量并使之不在子例程中。但是Guess的所有工作都只是Guess的输出,而Guess.get()并没有定义Guess。
此代码适用于我正在做的编程项目。这是gui歌曲测验:如果用户第一次正确使用它的3点,第二次1点和第三次关闭程序。这是在python 3.7上运行的