Python之学习笔记(GUI设计)

来源:互联网 发布:银光软件 编辑:程序博客网 时间:2024/05/21 22:52

  • GUI
    • 小试牛刀
    • 进阶

GUI

小试牛刀

# -*- coding: utf-8 -*-from Tkinter import *# 导入Tkinter包的所有内容'''GUI的库有TK,wxWidgets,Qt,GTKTk会调用操作系统提供的本地GUI接口,完成最终的GUI。我们编写的Python代码会调用内置的Tkinter,Tkinter封装了访问Tk的接口;Tk是一个图形库,支持多个操作系统,使用Tcl语言开发;Tk会调用操作系统提供的本地GUI接口,完成最终的GUI。所以,我们的代码只需要调用Tkinter提供的接口就可以了。'''# 第二步是从Frame派生一个Application类,这是所有Widget的父容器:# 在GUI中,每个Button、Label、输入框等,都是一个Widget。# Frame则是可以容纳其他Widget的Widget,所有的Widget组合起来就是一棵树。# pack()方法把Widget加入到父容器中,并实现布局。# pack()是最简单的布局,grid()可以实现更复杂的布局。# 在createWidgets()方法中,我们创建一个Label和一个Button,当Button被点击时,# 触发self.quit()使程序退出class Application(Frame):    def __init__(self, master=None):        Frame.__init__(self, master)        self.pack()        self.createWidgets()    def createWidgets(self):        self.helloLabel = Label(self, text='Hello, world!')        self.helloLabel.pack()        self.quitButton = Button(self, text='Quit', command=self.quit)        self.quitButton.pack()# 第三步是实例化Application,并启动消息循环app = Application()# 设置窗口标题:app.master.title('Hello World')# 主消息循环:GUI程序的主线程负责监听来自操作系统的消息,并依次处理每一条消息。# 因此,如果消息处理非常耗时,就需要在新线程中处理。app.mainloop()

进阶

# -*- coding: utf-8 -*-from Tkinter import *import tkMessageBoxclass 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.alertButton = Button(self, text='Hello', command=self.hello)        self.alertButton.pack()    def hello(self):        name = self.nameInput.get() or 'world'        tkMessageBox.showinfo('Message', 'Hello, %s' % name)# 第三步是实例化Application,并启动消息循环app = Application()# 设置窗口标题:app.master.title('Hello World')# 主消息循环:GUI程序的主线程负责监听来自操作系统的消息,并依次处理每一条消息。# 因此,如果消息处理非常耗时,就需要在新线程中处理。app.mainloop()
0 0