码迷,mamicode.com
首页 > 编程语言 > 详细

python图形界面

时间:2017-07-29 18:54:05      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:lob   图形界面   ext   create   nbsp   ram   try   输入框   设置   

简单介绍如何使用Tkinter进行GUI编程。

from tkinter import *
import tkinter.messagebox as messagebox
‘‘‘在GUI中,每个Button、Label、输入框等,都是一个Widget。
Frame则是可以容纳其他Widget的Widget,所有的Widget组合起来就是一棵树。
pack()方法把Widget加入到父容器中,并实现布局。
pack()是最简单的布局,grid()可以实现更复杂的布局。
在createWidgets()方法中,我们创建一个Label,两个Button,一个文本框。当Quit被点击时,触发self.quit()使程序退出,
当hello被点击时,触发self.hello(),显示message界面‘‘‘
class Application(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.pack()
        self.createWidgets()
    def createWidgets(self):
        self.nameInput=Entry(self)
        self.nameInput.pack()
        self.helloLabel=Label(self,text=hello world)
        self.helloLabel.pack()
        self.helloButton=Button(self,text=hello,command=self.hello)
        self.helloButton.pack()
        self.quitButton=Button(self,text=Quit,command=self.quit)
        self.quitButton.pack()
    def hello(self):
        name=self.nameInput.get() or world
        messagebox.showinfo(Message,Hello %s%name)
if __name__ == __main__:
    app=Application()
    ‘‘‘设置窗口标题‘‘‘
    app.master.title=liuruiqing
    ‘‘‘主程序循环‘‘‘
    app.mainloop()

 

python图形界面

标签:lob   图形界面   ext   create   nbsp   ram   try   输入框   设置   

原文地址:http://www.cnblogs.com/ruiqingliu/p/7257077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!