Python的第一个GUI程序

Tkinter是Python的标准GUI库,简单实用,适合初学者。
下面利用Tkinter来实现第一个简单的GUI程序。
实现目标:
在一个文本框(tkinter.Entry)中用键盘输入一段信息,通过鼠点击按钮控件(tkinter.Button)将这段信息打印在列表框(tkinter.Listbox)里。
直接上代码:

@author: idol
import tkinter as tk
class MyWindow(object):
    def __init__(self):
        self.top=tk.Tk()
        self.top.title('my window'.title())
        self.enter_box=tk.Entry(self.top,width=30)
        self.list_box=tk.Listbox(self.top,width=50)
        self.button1=tk.Button(self.top,text='Go',command=self.do,width=20)
        self.button2=tk.Button(self.top,text='clear'.title(),command=self.clearfuc,width=20)
        self.enter_box.pack()
        self.list_box.pack()
        self.button1.pack()
        self.button2.pack()
    def do(self):
        self.list_box.insert(tk.END,self.enter_box.get())
    def clearfuc(self):
        self.list_box.delete(0,tk.END)
def main():
    window=MyWindow()
    tk.mainloop()
if __name__=='__main__':
    main()

GUI界面如下:


Python的第一个GUI程序_第1张图片
GUI.PNG

你可能感兴趣的:(Python的第一个GUI程序)