首页 > 解决方案 > tkinter 不能 .get() 文本变量

问题描述

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 子例程。

当您单击猜测子程序 corequiz 的按钮时,我如何才能让guess.get() 从输入框中实际获取变量。

我尝试过制作全局变量并使其不在子程序中。但是这些都不起作用 Guess 只是输出 Guess 不是从 Guess.get() 定义的。

这段代码适用于我正在做的编程项目。这是gui song quiz:如果用户第一次做对了它的3分,第二个1分和第三次关闭程序。这是在 python 3.7 上运行的

标签: pythonpython-3.xtkinter

解决方案


推荐阅读