15.Python入门之图形用户界面

前言:本章主要学习GUI(Graphical User Interface)编程,GUI即带有按钮,文本等窗口的编程。Python有众多的GUI工具包,自带一个简单的GUI工具包,即EasyGui。当程序导入该工具包,则GUI操作变为调用相应的函数。


安装EasyGui


1.解压easygui-0.96.zip

2.使用命令窗口切换到easygui-docs-0.96的目录下

3.在Windows下执行C:\Python34\python.exe setup.py.install


导入EasyGui


使用前要导入相应模块,如下


>>>import easygui

>>>easygui.msgbox("hello")


回车即弹出信息框,但此方法要求在函数前面加上前缀easygui


另一种方法是导入整个包,则不必加上前缀


>>>from easygui import *

>>>msgbox("hello")

第三种方法如下(推荐)


>>>import easygui as g

>>>g.msgbox('hello')

该方法能保持EasyGui的命名空间,同时减少输入字符数量


使用EasyGui


import easygui as g

import sys


while 1:

    g.msgbox("hello")

msg = 'learn what'  #提示语句

title = 'interaction' #标题

choices = ["run","walk"] #可供选择项,列表显示

choice = g.choicebox(msg,title,choices)

g.msgbox("Your choice:" + str(choice),"result")

#note that we convert choice to string,in case

#the user cancelled the choice,and we got None

msg = "restart?"

title = "choose"

if g.ccbox(msg,title):  #show a Continue/Cancel dialog

     pass    #chose Continue

else:

     sys.exit(0) #user chose Cancel


上例仅显示了msgbox,choicebox和ccbox组件的用法


修改GUI默认设置


默认下显示的对话框很大,且字体不美观,手动调整参数


修改位置:C:\Python34\Lib\site-packages\easygui.py


更改对话框尺寸:找到def_ _choicebox,修改root_width和root_height.


更改字体:修改PROPORTIONAL_FONT_FAMILY的值

猜你喜欢

转载自blog.csdn.net/lwz45698752/article/details/78813037