'让'返回空值python

时间:2015-07-04 07:24:43

标签: python python-2.7 tkinter

我试过这段代码,但是我得到get命令的空白响应? 它只是在课堂上我得到空白的价值..但如果我创造一个功能而不是一个班级我从文本中获得价值......

from Tkinter import *
from ttk import Button,Entry,Style
import pickle
class Home(Frame):
    def __init__(self,parent):
         Frame.__init__(self,parent)
         self.parent=parent
         self.initUI()

    def initUI(self):
        self.parent.title("HOME SCREEN")
        frame = Frame(self)
        global a
        global z


        self.pack(fill=BOTH, expand=1)
        label1=Label(frame,text="USERNAME",)
        label2=Label(frame,text="PASSWORD")
        text1=Entry(frame, show="*", width=15)
        text2=Entry(frame,width=15)
        login=Button(self,text="Login",command=self.load)
        register=Button(self,text='Register',command=self.dump)
        Quit=Button(self,text='Quit',command=self.quit)
        delete=Button(self,text='Delete Account',command=self.delete)
        showb=Button(self,text='Show Accounts',command=self.show)

        label1.pack(side=LEFT)
        text2.pack(side=LEFT, padx=5, pady=5)
        label2.pack(side=LEFT )
        text1.pack(side=LEFT, padx=5, pady=5)
        frame.pack(fill=BOTH, expand=1)

        Quit.pack(side=RIGHT ,padx=5, pady=5)
        register.pack(side=RIGHT)
        login.pack(side=RIGHT)
        delete.pack(side=RIGHT)
        showb.pack(side=RIGHT)

        a=text1.get()
        z=text2.get()

2 个答案:

答案 0 :(得分:0)

在GUI出现在屏幕上之前,您正在调用text1.get()text2.get(),因此在您尝试获取值之前,用户无法输入文本。您需要在用户有机会在窗口小部件中输入信息后调用的函数内移动这些调用。

答案 1 :(得分:-4)

把它想出来......

def initUI(self):
    self.parent.title("HOME SCREEN")


    frame = Frame(self)

    self.pack(fill=BOTH, expand=1)
    label1=Label(frame,text="USERNAME",)
    label2=Label(frame,text="PASSWORD")
    text1=Entry(frame, show="*", width=15,)
    text2=Entry(frame,width=15)
    login=Button(self,text="Login",command=self.load)
    register=Button(self,text='Register',command=self.dump)
    Quit=Button(self,text='Quit',command=self.quit)
    delete=Button(self,text='Delete Account',command=self.delete)
    showb=Button(self,text='Show Accounts',command=self.show)


    label1.pack(side=LEFT)
    text2.pack(side=LEFT, padx=5, pady=5)
    label2.pack(side=LEFT )
    text1.pack(side=LEFT, padx=5, pady=5)
    frame.pack(fill=BOTH, expand=1)

    Quit.pack(side=RIGHT ,padx=5, pady=5)
    register.pack(side=RIGHT)
    login.pack(side=RIGHT)
    delete.pack(side=RIGHT)
    showb.pack(side=RIGHT)
    global text1
    global text2
def dump(self):

    z=(text1.get())
    a=(text2.get())