'get'返回空值python [duplicate]

问题描述 投票:-3回答:2

这个问题在这里已有答案:

我试过这段代码,但是我得到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()
python python-2.7 tkinter
2个回答
0
投票

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


-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())
© www.soinside.com 2019 - 2024. All rights reserved.