【问题标题】:.get() on entry box returns nothing?输入框上的 .get() 什么都不返回?
【发布时间】:2014-01-25 23:05:30
【问题描述】:

当我尝试从代码中的输入框获取值时,它返回一个空值?我该如何解决这个问题?我已将输入框设置为字符串变量,但变量的值仍然没有?

import os
import sys
from Tkinter import *
import tkMessageBox

debug = Tk()    #prevenets error on 'StringVar()'
debug.withdraw()
#global varables
users_entry = StringVar()

def about():
tkMessageBox.showinfo("About", "...")
def convert():
test = users_entry.get()
print test
def root_window():
#creates main window
root_window = Tk()
root_window.title("convert to binary")
root_window.geometry("600x400")
root_window.resizable(width = FALSE, height =FALSE)
root_window.configure(background = "gray")
#aboutButton
#about_button = Button(root_window, text = "about", command = about, width = 50)
#about_button.grid(row = 0, column = 0, padx = 85, pady = 3)
#entry box for text
users_entry = StringVar()
text_entry = Entry(root_window, text = "test",textvariable = users_entry, width = 70)
text_entry.grid(row = 1, column = 0, pady = 3)
#convert button
convert_button = Button(root_window, text = "convert", command = convert)
convert_button.grid()
root_window.mainloop()
root_window()
debug.mainloop()

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    需要设置StringVar对象的master:

    users_entry = StringVar(root_window, value="ok")
    

    【讨论】:

      【解决方案2】:

      您在评论中将users_entry 称为“全局变量”,然后在root_window 中定义一个新的本地users_entry

      convert中,users_entry将引用脚本顶部定义的那个,它仍然是一个空字符串,不是root_window中本地定义并绑定的那个到Entry 框。

      global users_entry 放在每个方法的顶部以实际使用您在脚本顶部初始化的版本。

      【讨论】:

        猜你喜欢
        • 2012-05-30
        • 2012-06-18
        • 2018-09-10
        • 2017-02-14
        • 2013-10-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多