Tkinter 中的文本输入 [英] Text Input in Tkinter

查看:52
本文介绍了Tkinter 中的文本输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

我正在尝试编写一个基本文件,我可以将其导入到所有其他程序中,该文件将具有一个简单的函数,该函数将从用户那里获取条目然后返回它.

I am trying to write a basic file which I can import in all other programs that will have a simple function that will take entry from the user and then return it.

代码

为此,我有以下代码:

class takeInput(object):

    def __init__(self,requestMessage,parent):

        self.string = ''
        self.frame = Frame(parent)
        self.frame.pack()        
        self.acceptInput(requestMessage)

    def acceptInput(self,requestMessage):

        r = self.frame

        k = Label(r,text=requestMessage)
        k.pack(side='left')
        self.e = Entry(r,text='Name')
        self.e.pack(side='left')
        self.e.focus_set()
        b = Button(r,text='okay',command=self.gettext)
        b.pack(side='right')

    def gettext(self):
        self.string = self.e.get()
        self.frame.destroy()
        print self.string

    def getString(self):
        return self.string

def getText(requestMessage,parent):
    global a
    a = takeInput(requestMessage,parent)
    return a.getString()

我还添加了一些脚本级别的代码来测试这个:

And I also added some script level code so as to test this:

root = Tk()

getText('enter your name',root)

var = a.getString()

print var

root.mainloop()

真正让我困惑的是:

  1. var 没有我输入的值 它有空字符串 ''
  2. a.string 变量具有我输入的值,并且我从 shell 中检查了它.
  1. var does not have the value that I entered it has the empty string ''
  2. a.string variable has the value that I entered and I checked this from the shell.

当我尝试将 a.getString() 返回的字符串分配给 shell 中的 var 时,它起作用了.

Also When I tried to assign the string returned from a.getString() to var in the shell, then it worked.

注意我是 Tkinter 编程的新手,不完全了解 mainloop() 的工作原理.所以也许这就是问题所在.但我不确定.

note I am new to Tkinter programming and dont fully understand how the mainloop() works. So maybe this is were the problem is. But I am not sure.

规格

操作系统:Linux Mint 14

OS:Linux Mint 14

Python 空闲 2.7

Python IDLE 2.7

帮我解决这个问题.

推荐答案

你的代码流程是这样的:

The flow of your code goes like this:

  • 主要作用域调用 getText.
  • getText 创建一个 takeInput 对象 a.
  • takeInput 对象初始化自身,创建标签 &按钮等
  • getText 返回 a.getString(),它返回 self.string,它仍然有它的默认值,空字符串.
  • 主作用域打印 var,这是空的.
  • the main scope calls getText.
  • getText creates a takeInput object a.
  • the takeInput object initializes itself, creating Labels & buttons etc.
  • getText returns a.getString(), which returns self.string, which still has its default value, the empty string.
  • the main scope prints var, which is empty.

到目前为止,所有这一切都发生在几纳秒的范围内.用户甚至还没有看到窗口.

So far, all of this has taken place within the span of a few nanoseconds. The user hasn't even seen the window yet.

主作用域然后调用root.mainloop(),这最终给了用户与窗口交互的机会.但为时已晚.var 已经被打印出来了.

the main scope then calls root.mainloop(), which finally gives the user the opportunity to interact with the window. But it's too late. var has already been printed.

如果您希望 getText 在用户提交文本之前不返回,那么 mainloop 必须发生在 getText 内部,而不是在它之后.

If you want getText to not return until the user has submitted his text, then mainloop has to occur inside getText, not after it.

from Tkinter import *

class takeInput(object):

    def __init__(self,requestMessage):
        self.root = Tk()
        self.string = ''
        self.frame = Frame(self.root)
        self.frame.pack()        
        self.acceptInput(requestMessage)

    def acceptInput(self,requestMessage):
        r = self.frame

        k = Label(r,text=requestMessage)
        k.pack(side='left')
        self.e = Entry(r,text='Name')
        self.e.pack(side='left')
        self.e.focus_set()
        b = Button(r,text='okay',command=self.gettext)
        b.pack(side='right')

    def gettext(self):
        self.string = self.e.get()
        self.root.destroy()

    def getString(self):
        return self.string

    def waitForInput(self):
        self.root.mainloop()

def getText(requestMessage):
    msgBox = takeInput(requestMessage)
    #loop until the user makes a decision and the window is destroyed
    msgBox.waitForInput()
    return msgBox.getString()

var = getText('enter your name')
print "Var:", var

这篇关于Tkinter 中的文本输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆