Python tkinter - 为Entry()调用字符串输入

时间:2017-05-31 21:36:11

标签: python if-statement tkinter tkinter-entry

我正在学习Python tkinter基础知识,并且无法将我的方法称为"提交()"争论我的Entry1变量的字符串值。 我已经尝试了Entry1的.get()方法,但是控制台说Entry1的get属性不存在。

from tkinter import *

Window = Tk()

def Submit():
    Answer = Entry1.text
    if Answer == "byte":
    print("correct")

Label(Window, text="What do you call 8 bits?").grid(row=0)
Entry1 = Entry(Window, text="").grid(row=1)
Button(Window, text="SUBMIT", command=Submit).grid(row=2)

Window.mainloop()

1 个答案:

答案 0 :(得分:2)

我明白了。不过,谢谢。

from tkinter import *
Window = Tk()

def Quiz():
   if Answer1.get() == "8" :
       print("correct")

Question1 = Label(Window, text="How many bits are in a     Byte?").grid(row=0)
Answer1 = StringVar()
Entry1 = Entry(Window, textvariable=Answer1).grid(row=1)
Button1 = Button(Window, text="ANSWER", command=Quiz).grid(row=2)

Window.mainloop()