如何使条目小部件中的文本保存到变量?

时间:2015-08-03 20:50:28

标签: python python-2.7 tkinter

我对整个Python都很陌生,更不用说Tkinter了。我想知道是否可以将我在输入窗口小部件中输入的文本保存到变量中,这样我就可以在ping脚本中运行它,然后让标签显示结果。

import Tkinter

class pinger(Tkinter.Tk):
    def __init__(self, parent):
        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self, textvariable=self.entryVariable)
        self.entry.grid(column=0,row=0,sticky="EW")
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable.set(u"")

        button = Tkinter.Button(self,text="Ping",command=self.OnButtonClick)
        button.grid(column=1,row=0)

        self.labelVariable = Tkinter.StringVar()
        label= Tkinter.Label(self,textvariable=self.labelVariable,anchor="w",fg="black",bg="white")
        label.grid(column=0,row=1,columnspan=2,sticky="EW")
        self.labelVariable.set(u"Enter a hostname to begin")


        self.resizable(False,False)

    def OnButtonClick(self):
        self.labelVariable.set( self.entryVariable.get()+" (Button clicked)" )

    def OnPressEnter(self,event):
        self.labelVariable.set( self.entryVariable.get()+" (Enter Pressed)" )



if __name__ == "__main__":
    app = pinger(None)
    app.title('Server Pinger')
    app.mainloop()

0 个答案:

没有答案