2014-01-25 41 views
0

當我嘗試從我的代碼中的輸入框中獲取值時,它會返回一個空值?,我該如何解決這個問題?,我已將輸入框設置爲字符串變量,但變量的值保持不變?.get()在輸入框上什麼都不返回?

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

回答

0

你需要設置StringVar對象的主人:

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

您在您的評論稱users_entry一個「全局變量」,但隨後定義一個新的,局部users_entryroot_window

convert,所述users_entry將指的是一個在腳本的頂部限定的,這仍然是一個空字符串,root_window並綁定到Entry框本地定義的一個。

global users_entry放在每個方法的頂部,實際上是使用您在腳本頂部初始化的版本。