Tkinter中的Python live Entry

时间:2012-10-30 20:55:38

标签: python tkinter tkinter-entry

我希望看到Tkinter Entry值。当我打字时,我看到打印'downkey';。如果我正在键入4个字符,那么我看到一个条目在窗口中显示确定,好的,好的,好的等等。

from email . mime . multipart import MIMEMultipart
from email . mime . text import MIMEText
from Tkinter import *
from ttk import *

import ConfigParser
import smtplib

class Example ( Frame ):

    def __init__ ( self, parent ):

        self . ini ()
        Frame . __init__ ( self, parent )
        self . parent = parent
        self . initUI ()

    def initUI ( self ):

        self . parent . title ( "Quit button" )
        self . style = Style ()
        self . style . theme_use ( "default" )
        self . pack ( fill = BOTH, expand = 1 )
        inputfield = Entry ( self ) # I would like see this entry the app. window bottom in live, when i'm typewriter. If i key down/press call one function is class.
        inputfield . place ( x = 10, y = 10 )
        quitButton = Button ( self, text = "Quit", command = self . quit )
        quitButton . place ( x = 50, y = 50 )

def main ():

    root = Tk ()
    root . geometry ( "250x150+300+300" )
    app = Example ( root )
    root . mainloop ()

if __name__ == '__main__':

    main ()

1 个答案:

答案 0 :(得分:1)

如果要在输入字段中输入后调用函数,则必须使用事件处理程序并将输入字段绑定到事件,如按回车和某些函数xyz。 这意味着在按下return之后,可以调用函数xyz。

要根据条目更新消息,您需要一个变量字符串作为条目参数。

全局定义文本变量:

m=StringVar()

在Eample类中添加:

您可以将变量文本添加到输入字段

inputfield = Entry ( self,textvariable=m )

self.inputfield.bind('<Return>',self.xyz)  #to call xyz which you may want to define

并将textvariable添加到消息中以进行更新

message = Message(self,textvariable=m) #to update continuously the message which i hope you meant by "live"