【问题标题】:'get' returning blank value python [duplicate]'get'返回空白值python [重复]
【发布时间】:2015-09-21 23:06:50
【问题描述】:

我尝试了这段代码,但我从 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()

【问题讨论】:

  • 你在使用 TKinter 吗?进口会很好......
  • 是的,我正在使用 Tkinter
  • 我不是傻瓜不导入它.....我使用 get 函数从条目中获取空白值

标签: python python-2.7 tkinter


【解决方案1】:

我自己想通了...

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-25
      • 2018-02-04
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-20
      相关资源
      最近更新 更多