• Python EasyGui Tutorial 翻译


    英文原文:http://easygui.sourceforge.net/tutorial.html#msgbox

    参考:http://www.itnose.net/detail/6107761.html (小甲鱼)

    EasyGui 下载地址:http://sourceforge.net/projects/easygui/files/0.97/

    演示使用Python 2.7.9

    安装:【Windows下】执行C:\Python27\python.exe setup.py install

    1,一个简单的小程序

    from easygui import *
    while 1:
        msgbox("Hello, world!")
    
        msg ="What is your favorite flavor?"
        title = "Ice Cream Survey"
        choices = ["Vanilla", "Chocolate", "Strawberry", "Rocky Road"]
        choice = choicebox(msg, title, choices)
    
        # note that we convert choice to string, in case
        # the user cancelled the choice, and we got None.
        msgbox("You chose: " + str(choice), "Survey Result")
    
        msg = "Do you want to continue?"
        title = "Please Confirm"
        if ccbox(msg, title):     # show a Continue/Cancel dialog
            pass  # user chose Continue
        else:
            sys.exit(0)           # user chose Cancel

    2,导入EasyGui模块,3种调用方式

    import easygui
    easygui.msgbox(...) # 调用
    from easygui import *
    msgbox(...) #调用
    import easygui as g
    g.msgbox(...) #调用

    3,按钮组建

    msgbox(),ccbox(),ynbox(),buttonbox(),indexbox(),boolbox()   一共6个函数

    msgbox():显示信息,并提供一个OK按钮(按钮名字可编辑)

    ccbox():提供一个继续(Continue)和一个删除(Cancel)选择,同时返回True(表示Continue)或者False(表示Cancel)

    ynbox():提 Yes 和 No 选择,同时返回True(表示Yes)或者False(表示No)

    buttonbox():显示信息,标题,可设置按钮 ,返回用户选择的按钮信息

    indexbox():基本跟上buttonbox()类似,区别就是当用户选择第一个按钮的时候返回序号 0, 选择第二个按钮的时候返回序号 1

    boolbox() :如果第一个按钮被选中则返回 1,否则返回 0

    4,用户选择

    choicebox(),multchoicebox()  一共2个函数

    choicebox():为用户提供了一个可选择的列表,使用序列(元祖或列表)作为选项,这些选项显示前会按照不区分大小写的方法排好序

    multchoicebox() :函数也是提供一个可选择的列表,与 choicebox() 不同的是,multchoicebox() 支持用户选择 0 个,1 个或者同时选择多个选项

    5,用户输入信息

    enterbox(),integerbox(),multenterbox() 一共3个函数

    enterbox():为用户提供一个最简单的输入框,返回值为用户输入的字符串。默认返回的值会自动去除首尾的空格,如果需要保留首尾空格的话请设置参数 strip=False

    integerbox():为用户提供一个简单的输入框,用户只能输入范围内(lowerbound参数设置最小值,upperbound参数设置最大值)的整型数值,否则会要求用户重新输入

    multenterbox() :

    为用户提供多个简单的输入框,要注意以下几点:

  • 如果用户输入的值比选项少的话,则返回列表中的值用空字符串填充用户为输入的选项。
  • 如果用户输入的值比选项多的话,则返回的列表中的值将截断为选项的数量。
  • 如果用户取消操作,则返回域中的列表的值或者None值。
  • 6,用户输入密码

    passwordbox(),multpasswordbox() 一共2个函数

    passwordbox():跟 enterbox() 样式一样,不同的是用户输入的内容用"*"显示出来,返回用户输入的字符串

    multpasswordbox():跟 multenterbox() 使用相同的接口,但当它显示的时候,最后一个输入框显示为密码的形式("*")

    7,显示文本

    textbox(),codebox() 一共2个函数

    textbox():函数默认会以比例字体(参数 codebox=1 设置为等宽字体)来显示文本内容(会自动换行哦),这个函数适合用于显示一般的书面文字

    注:text 参数(第三个参数)可以是字符串类型,列表类型,或者元祖类型。

    codebox():以等宽字体显示文本内容,相当于 textbox(codebox=1)

    8,目录与文件

    diropenbox(),fileopenbox(),filesavebox() 一共3个函数

    diropenbox() :函数用于提供一个对话框,返回用户选择的目录名(带完整路径哦),如果用户选择"Cancel"则返回 None

    fileopenbox() :函数用于提供一个对话框,返回用户选择的文件名(带完整路径哦),如果用户选择"Cancel"则返回 None

    filesavebox(): 函数提供一个对话框,让用于选择文件需要保存的路径(带完整路径哦),如果用户选择"Cancel"则返回 None

    9,记住用户的设置

    GUI 编程中一个常见的场景就是要求用户设置一下参数,然后保存下来,以便下次用户使用你的程序的时候可以记住他的设置。
    为了实现对用户的设置进行存储和恢复这一过程,EasyGui 提供了一个叫做 EgStore 的类。为了记住某些设置,你的应用程序必须定义一个类(暂时称之为"设置"类,尽管你随意地使用你想要的名称设置它)继承自 EgStore 类。
    然后你的应用程序必须创建一个该类的对象(暂时称之为"设置"对象)。
    设置类的构造函数(__init__ 方法)必须初始化所有的你想要它所记住的那些值。
    一旦你这样做了,你就可以在"设置"对象中通过设定值去实例化变量,从而很简单地记住设置。之后使用 settings.store() 方法在硬盘上持久化设置对象。
    下面是创建一个"设置"类的例子:

    #-----------------------------------------------------------------------
    # define a class named Settings as a subclass of EgStore
    #-----------------------------------------------------------------------
    class Settings(EgStore):
    
        def __init__(self, filename):  # filename is required
            #-------------------------------------------------
            # Specify default/initial values for variables that
            # this particular application wants to remember.
            #-------------------------------------------------
            self.userId = ""
            self.targetServer = ""
    
            #-------------------------------------------------
            # For subclasses of EgStore, these must be
            # the last two statements in  __init__
            #-------------------------------------------------
            self.filename = filename  # this is required
            self.restore()            # restore values from the storage file if possible
    #-----------------------------------------------------------------------
    # create "settings", a persistent Settings object
    # Note that the "filename" argument is required.
    # The directory for the persistent file must already exist.
    #-----------------------------------------------------------------------
    settingsFilename = os.path.join("C:", "myApp", "settings.txt")  # Windows example
    settings = Settings(settingsFilename)
    # we initialize the "user" and "server" variables
    # In a real application, we'd probably have the user enter them via enterbox
    user    = "obama_barak"
    server  = "whitehouse1"
    
    # we save the variables as attributes of the "settings" object
    settings.userId = user
    settings.targetServer = server
    settings.store()    # persist the settings
    
    # run code that gets a new value for userId
    # then persist the settings with the new value
    user    = "biden_joe"
    settings.userId = user
    settings.store()
    10,捕获异常

    用 EasyGui 编写 GUI 程序,有时候难免会产生异常。当然这取决于你如何运行你的应用程序,当你的应用程序崩溃的时候,堆栈追踪可能会被抛出,或者被写入到 stdout 标准输出函数中。
    EasyGui 通过 exceptionbox() 函数提供了更好的方式去处理异常,异常出现的时候,exceptionbox() 会显示堆栈追踪在一个 codebox() 中并且允许你做进一步的处理。
    exceptionbox() 很容易使用,下面是一个例子:

    try:
        someFunction()  # this may raise an exception
    except:
        exceptionbox()
  • 相关阅读:
    Java 和 DynamoDB
    关于Mongodb的全面总结
    utf8mb4 使用注意
    mysql 事务隔离讲的比较好的文章收藏。
    [mysql] 常用命令总结
    [JTA] Java事务api
    [Hibernate] Hibernate 参数设置一览表(转)
    Spring配置sessionFactory的几种常用方式
    [前端] org.apache.jasper.JasperException 页面有空引用
    [Hibernate] JPA与Hibernate的优缺点
  • 原文地址:https://www.cnblogs.com/temo/p/5160827.html
  • Copyright © 2020-2023  润新知