美文网首页
Python的第一个GUI程序

Python的第一个GUI程序

作者: 龙城小牧 | 来源:发表于2018-08-04 13:40 被阅读10次

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界面如下:


GUI.PNG

相关文章

网友评论

      本文标题:Python的第一个GUI程序

      本文链接:https://www.haomeiwen.com/subject/ercmvftx.html