600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > python编程基础:python 实现GUI(图形用户界面)编程详解

python编程基础:python 实现GUI(图形用户界面)编程详解

时间:2018-10-13 02:47:36

相关推荐

python编程基础:python 实现GUI(图形用户界面)编程详解

今天小编就为大家分享一篇python 实现GUI(图形用户界面)编程详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

Python支持多种图形界面的第三方库,包括:

wxWidgets

Qt

GTK

Tkinter: Tkinter 模块(Tk 接口)是 Python 的标准 Tk GUI 工具包的接口 .Tk 和 Tkinter 可以在大多数的 Unix 平台下使用,同样可以应用在 Windows 和 Macintosh 系统里。Tk8.0 的后续版本可以实现本地窗口风格,并良好地运行在绝大多数平台中。

wxPython:wxPython 是一款开源软件,是 Python 语言的一套优秀的 GUI 图形库,允许 Python 程序员很方便的创建完整的、功能键全的 GUI 用户界面。

Jython:Jython 程序可以和 Java 无缝集成。除了一些标准模块,Jython 使用 Java 的模块。Jython 几乎拥有标准的Python 中不依赖于 C 语言的全部模块。比如,Jython 的用户界面将使用 Swing,AWT或者 SWT。Jython 可以被动态或静态地编译成 Java 字节码。

Tkinter

我们来梳理一下概念:

我们编写的Python代码会调用内置的Tkinter,Tkinter封装了访问Tk的接口;

Tk是一个图形库,支持多个操作系统,使用Tcl语言开发;

Tk会调用操作系统提供的本地GUI接口,完成最终的GUI。

所以,我们的代码只需要调用Tkinter提供的接口就可以了。

在GUI中,每个Button、Label、输入框等,都是一个Widget。Frame则是可以容纳其他Widget的Widget,所有的Widget组合起来就是一棵树。

pack()方法把Widget加入到父容器中,并实现布局。pack()是最简单的布局,grid()可以实现更复杂的布局。

Tkinter

创建一个GUI程序

1、导入 Tkinter 模块

2、创建控件

3、指定这个控件的 master, 即这个控件属于哪一个

4、告诉 GM(geometry manager) 有一个控件产生了。

实例:

#!/usr/bin/python# -*- coding: UTF-8 -*-import Tkintertop = Tkinter.Tk()# 进入消息循环top.mainloop()

组件

Tkinter的提供各种控件,如按钮,标签和文本框,一个GUI应用程序中使用。这些控件通常被称为控件或者部件。

目前有15种Tkinter的部件。我们提出这些部件以及一个简短的介绍,在下面的表:

标准属性

标准属性也就是所有控件的共同属性,如大小,字体和颜色等等。

几何管理

Tkinter控件有特定的几何状态管理方法,管理整个控件区域组织,一下是Tkinter公开的几何管理类:包、网格、位置

#!/usr/bin/env pythonimport osfrom time import sleepfrom Tkinter import *class DirList(object):def __init__(self, initdir=None):self.top = Tk()self.label = Label(self.top,text='Directory Lister v1.2')self.label.pack()self.cwd=StringVar(self.top)self.dirl = Label(self.top, fg='blue',font=('Helvetica', 12, 'bold'))self.dirl.pack()self.dirfm = Frame(self.top)self.dirsb = Scrollbar(self.dirfm)self.dirsb.pack(side=RIGHT, fill=Y)self.dirs = Listbox(self.dirfm, height=15,width=50, yscrollcommand=self.dirsb.set)self.dirs.bind('<Double-1>', self.setdirandgo)self.dirsb.config(command=self.dirs.yview)self.dirs.pack(side=LEFT, fill=BOTH)self.dirfm.pack()self.dirn = Entry(self.top, width=50,textvariable=self.cwd)self.dirn.bind('<Return>', self.dols)self.dirn.pack()self.bfm = Frame(self.top)self.clr = Button(self.bfm, text='Clear',command=self.clrdir,activeforeground='white',activebackground='blue')self.ls = Button(self.bfm,text='List Directory',command=self.dols,activeforeground='white',activebackground='green')self.quit = Button(self.bfm, text='Quit',command=self.top.quit,activeforeground='white',activebackground='red')self.clr.pack(side=LEFT)self.ls.pack(side=LEFT)self.quit.pack(side=LEFT)self.bfm.pack()if initdir:self.cwd.set(os.curdir)self.dols()def clrdir(self, ev=None):self.cwd.set('')def setdirandgo(self, ev=None):self.last = self.cwd.get()self.dirs.config(selectbackground='red')check = self.dirs.get(self.dirs.curselection())if not check:check = os.curdirself.cwd.set(check)self.dols()def dols(self, ev=None):error = ''tdir = self.cwd.get()if not tdir:tdir = os.curdirif not os.path.exists(tdir):error = tdir + ': no such file'elif not os.path.isdir(tdir):error = tdir + ': not a directory'if error:self.cwd.set(error)self.top.update()sleep(2)if not (hasattr(self, 'last') \and self.last):self.last = os.curdirself.cwd.set(self.last)self.dirs.config(selectbackground='LightSkyBlue')self.top.update()returnself.cwd.set('FETCHING DIRECTORY CONTENTS...')self.top.update()dirlist = os.listdir(tdir)dirlist.sort()os.chdir(tdir)self.dirl.config(text=os.getcwd())self.dirs.delete(0, END)self.dirs.insert(END, os.curdir)self.dirs.insert(END, os.pardir)for eachFile in dirlist:self.dirs.insert(END, eachFile)self.cwd.set(os.curdir)self.dirs.config(selectbackground='LightSkyBlue')def main():d = DirList(os.curdir)mainloop()if __name__ == '__main__':main()

最后给大家推荐一个口碑不错的python聚集地【点击进入】,这里有很多的老前辈学习技巧,学习心得

,面试技巧,职场经历等分享,更为大家精心准备了零基础入门资料,实战项目资料,每天都有程序员

定时讲解Python技术,分享一些学习的方法和需要留意的小细节

以上这篇python 实现GUI(图形用户界面)编程详解就是小编分享给大家的全部内容了

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。