零基础入门学习Python(35)--图形用户界面入门:EasyGui

本文详细介绍了Python的EasyGui库,包括如何不在IDLE中运行、基本使用示例、各种功能演示,如msgbox、ccbox、ynbox、buttonbox等。此外,还讲解了如何导入和使用EasyGui,以及如何利用EasyGui实现用户交互,如输入框、密码框、文件和目录选择等。最后提到了EasyGui的异常处理和EgStore类用于保存用户设置。
摘要由CSDN通过智能技术生成

知识点

EasyGui学习文档【超详细中文版】

1. 建议不要在IDLE上运行EasyGui

EasyGui是运行在TKinter上并拥有自身的事件循环,而IDLE也是Tkinter写的一个应用程序并也拥有自身的事件循环。因此当两者同时运行的时候,有可能会发生冲突,且带来不可预测的结果。因此如果你发现你的EasyGui程序有这样的问题,请尝试在IDLE外去运行你的程序。

2. 一个简单的例子

在EasyGui中,所有的GUI互动均是通过简单的函数调用,下边一个简单的例子告诉你EasyGui确实很Easy!

import easygui as g
import sys

while 1:
        g.msgbox("嗨,欢迎进入第一个界面小游戏^_^")

        msg ="请问你希望在鱼C工作室学习到什么知识呢?"
        title = "小游戏互动"
        choices = ["谈恋爱", "编程", "OOXX", "琴棋书画"]

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

        # note that we convert choice to string, in case
        # the user cancelled the choice, and we got None.
        g.msgbox("你的选择是: " + str(choice), "结果")

        msg = "你希望重新开始小游戏吗?"
        title = "请选择"

        if g.ccbox(msg, title):     # show a Continue/Cancel dialog
                pass  # user chose Continue
        else:
                sys.exit(0)     # user chose Cancel

3. EasyGui各种功能演示

要运行EasyGui的演示程序,在命令行调用EasyGui是这样的:

python easygui.py

或者你可以从IDE工具上调用:

import easygui as g
g.egdemo()

成功调用后你将可以尝试EasyGui拥有的各种功能,并将你选择的结果打印至控制台。

4. 导入EasyGui

为了使用EasyGui这个模块,你应该先导入它。最简单的导入语句是:

import easygui

如果你使用上面这种形式导入的话,那么你使用EasyGui的函数的时候,必须在函数的前面加上前缀easygui,像这样:

easygui.msgbox(...)

另一种选择是导入整个EasyGui包:

from easygui import *

这使得我们更容易调用EasyGui的函数,你可以直接这样编写代码:

msgbox(...)

第三种方案是使用类似下边的import语句:

import easygui as g

这样让你保持EasyGui的命名空间,同时减少你的打字数量。导入之后你就可以这么调用EasyGui的函数:

g.msgbox(...)

5. 使用EasyGui

一旦你的模块导入EasyGui,GUI操作就是一个简单的调用EasyGui函数的几个参数的问题了。
例如,使用EasyGui来实现著名的你好,世界!程序是这样的:

import easygui as g
g.msgbox("Hello,World!")

6. EasyGui函数的默认参数

对于所有函数而言,前两个参数是消息和标题。按照这个规律,在某种情况下,这可能不是最有利于用户的安排(例如,对话框在获取目录和文件名的时候忽略消息参数),但是我觉得保持这种一致性贯穿于所有的窗口部件是一种更为重要的考虑!

绝大部分的EasyGui函数都有默认参数,几乎所有的部件都会显示一个消息和标题。标题默认是空字符串,消息通常有一个简单的默认值。

这使得你可以尽量少的去设置参数,比如msgbox()函数标题部分的参数是可选的,所以你调用msgbox()的时候可以只指定一个消息参数,例如:

import easygui as g
g.msgbox("Hello,World!")


当然你也可以指定标题参数和消息参数,例如:

import easygui as g
g.msgbox('Hello World!','小甲鱼编程')


在各类按钮组件里,默认的消息是Shall I continue?,所以你可以不带任何参数的去调用它们。这里我们演示不带任何参数的去调用ccbox(),当选择cancel关闭窗口的时候返回一个布尔类型的值:

import easygui as g
while True:
    if g.ccbox():
        pass         # user chose to continue
    else:
        exit()     # user chose to cancel

7. 使用关键字参数调用EasyGui函数

调用EasyGui函数还可以使用关键字参数
现在假设你需要使用一个按钮组件,但你不想指定标题参数(第二个参数),你仍可以使用关键字参数的方法指定choices参数(第三个参数),像这样:

import easygui as g
xz = ['棒','真棒','超级棒']
replay = g.choicebox('我棒不棒?',choices=xz)

8. 使用按钮组建

根据需求,EasyGui在buttonbox()上建立了一系列的函数供调用。

8.1 msgbox()
>>> help(g.msgbox)
Help on function msgbox in module easygui:

msgbox(msg='(Your message goes here)', title=' ', ok_button='OK', image=None, root=None)
    Display a messagebox

重写OK按钮最简单的方法是使用关键字参数:

import easygui as g
g.msgbox('我一定要学会Python', ok_button='加油')

8.2 ccbox()
>>> help(g.ccbox)
Help on function ccbox in module easygui:

ccbox(msg='Shall I continue?', title=' ', choices=('Continue', 'Cancel'), image=None)
    Display a msgbox with choices of Continue and Cancel.

    The default is "Continue".

    The returned value is calculated this way::
        if the first choice ("Continue") is chosen, or if the dialog is cancelled:
            return 1
        else:
            return 0

    If invoked without a msg argument, displays a generic request for a confirmation
    that the user wishes to continue.  So it can be used this way::

        if ccbox():
            pass # continue
        else:
            sys.exit(0)  # exit the program

    @arg msg: the msg to be displayed.
    @arg title: the window title
    @arg choices: a list or tuple of the choices to be displayed

ccbox()提供一个选择:Continue或者Cancel,并相应的返回1(选中Continue)或者0(选中Cancel)。

【注意】:ccbox()是返回整形的1或0,不是布尔类型的True或False。但你仍然可以这么些:

import sys
import easygui as g
if g.ccbox('要再来一次吗?',choices=('要啊要啊^_^', '算了吧T_T')):
    g.msgbox('不给玩了,再玩就玩坏了……')
else:
    sys.exit(0)

8.3 ynbox()
>>> help(g.ynbox)
Help on function ynbox in module easygui:

ynbox(msg='Shall I continue?', title=' ', choices=('Yes', 'No'), image=None)
    Display a msgbox with choices of Yes and No.

    The default is "Yes".

    The returned value is calculated this way::
        if the first choice ("Yes") is chosen, or if the dialog is cancelled:
            return 1
        else:
            return 0

    If invoked without a msg argument, displays a generic request for a confirmation
    that the user wishes to continue.  So it can be used this way::
        if ynbox(): pass # continue
        else: sys.exit(0)  # exit the program

    @arg msg: the msg to be displayed.
    @arg title: the window title
    @arg choices: a list or tuple of the choices to be displayed

同8.2

8.4 buttonbox()
>>> help(g.buttonbox)
Help on function buttonbox in module easygui:

buttonbox(msg='', title=' ', choices=('Button1', 'Button2', 'Button3'), image=None, root=None)
    Display a msg, a title, and a set of buttons.
    The buttons are defined by the members of the choices list.
    Return the text of the button that the user selected.

    @arg msg: the msg to be displayed.
    @arg title: the window title
    @arg choices: a list or tuple of the choices to be displayed

可以使用buttonbox()定义自己的一组按钮,buttonbox()会显示一组你定义好的按钮。

当用户点击任意一个按钮的时候,buttonbox()返回按钮的文本内容。如果用户取消或者关闭窗口,那么会返回默认选项(第一个选项)。请看示例:

import easygui as g
g.buttonbox(choices=('草莓','西瓜','芒果'))

8.5 indexbox()
>>> help(g.indexbox)
Help on function indexbox <
  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值