求教python高手赐给一个源代码

如题,新手刚学一个东西,想测试下,但是有段代码不是很会,请高手帮忙写下
很简答,就是一个文本框,一个按钮,当按下按钮是,就可以把文本框中的字符串赋值给变量

这是你想要的

#!/usr/bin/env python
#   Filename: appui.py
"""

.. moduleauthor:: ....
.. test module of Tkinter
"""
from Tkinter import *
import tkMessageBox

root = Tk()
root.geometry('850x40+80+80')  #设置窗体高宽与窗体相对屏幕左上角位置

class Appui(Frame):
    """
    docsting for Appui
    """

    def __init__(self, parent=None):
        """
        Initialize the Appui Class
        """
        Frame.__init__(self, width=850, height=60, bg='gray40')
        self.propagate(False)
        self.pack()
        self.input = StringVar()
        self.master.title('Test Widgets')
        self.master.iconname('Tk-42')
        self.buildFrame()  # Build the widgets


    def buildFrame(self):
        """
        make the frame and add widgets into the frame
        """    
        self.LabelF=Frame(self,width=850, height=60, bg='gray80')
        self.LabelF.propagate(False)
        self.LabelF.pack(side=TOP)
        Label(self.LabelF, fg='steelblue1', bg='gray80', text= "Text field description: ").pack(side=LEFT)
        #设置输入的widgets
        self.e0 = Entry(self.LabelF, width=80,  textvariable=self.input)
        self.e0.pack(side=LEFT)
        Button(self.LabelF, text='Get',  borderwidth=5, command=self.callback).pack(side=RIGHT,padx=10)
        

    def callback(self):
        """
        callback of get path Button       
        """
        try:
            #self.e0.delete(0,END)
            Value=self.input.get()
            if Value is not None:
                print "Get String from Entry" + Value
            else:
                self.e0.delete(0,END)
        except (RuntimeError, TypeError, NameError, ValueError, IOError) as e:
            tkMessageBox.showerror("Error function callback()", '%s' % e)

    def exit(self):
        """
        exit of process       
        """
        exit(0)

def main():
    """
    Main function
    """
    Appui().mainloop()

if __name__ == '__main__':
    main()

温馨提示:答案为网友推荐,仅供参考
相似回答