700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Python3 基础学习笔记4-图形用户界面(easygui)

Python3 基础学习笔记4-图形用户界面(easygui)

时间:2019-08-25 18:48:03

相关推荐

Python3 基础学习笔记4-图形用户界面(easygui)

1.安装 easygui模块:pip install easygui

2.导入 easygui模块

#方法一>>> import easygui>>> easygui.msgbox("第一种导入方法成功!")#方法二>>> from easygui import *>>> msgbox("第二种导入方法成功!")#方法三(推荐!)>>> import easygui as eg>>> eg.msgbox("第三种导入方法成功!")# CMD 中打开python easygui.py

3.按钮组件

(1)msgbox():显示一个消息并提供一个OK键

# msgbox(msg = '(Your message goes here)', tilte = ' ', ok_button = '0k', image = None, root = None) >>> import easygui as eg>>> eg.msgbox("boom!")'OK'>>> eg.msgbox("六人行,必有我师!", ok_button = "Monika bang!")'Monika bang!'

(2)ccbox():提供一个选择,返回布尔值 C[o]ntinue 中的 [o] 表示快捷键 o,点击键盘上的o,则表示单击 C[o]ntinue 按钮,C[a]ncel 同理

# ccbox(msg = 'Shall I continue?', title = ' ', choices = ('C[o]ntinue', 'C[a]ncel'), image = None, default_choice = 'C[o]ntinue', cancel_choice = 'C[a]ncel') >>> import easygui as eg>>> import sys>>> while 1 :...eg.msgbox("Welcome to the first game!")...msg = "What's your favorite fruit?"...title = "Q&A"...choices = ["banana", "apple", "strawberry"]...choice = eg.choicebox(msg, title, choices)...eg.msgbox("Your choice is :" + str(choice), "Result")...msg = "One more time!"...title = "Please choose your favorite!"...if box(msg, title):pass...else:... sys.exit(0)

(3)ynbox:[<F1>Yes]表示将键盘上的F1键作为Yes的快捷键

>>> ynbox(msg = 'Shall I continue?', title = ' ', choices = ('[<F1>Yes]', '[<F2>No]'), image = None, default_choice = '[<F1>Yes]', cancel_choice = '[<F2>No]')

(4)buttonbox():用户自定义一组按钮,单击任意按钮则返回该按钮的文本内容,取消或关闭窗口,那么返回默认选项,即第一个选项

# buttonbox(msg = ' ', title = ' ', choices = ('Button[1]', 'Button[2]', 'Button[3]'), image = None, images = None, default_choice = None, cancel_choice = None, callback = None, run = True)>>> import easygui as eg>>> eg.buttonbox("Whice one?", choices = ('banana', 'apple', 'grape'))

(5)indexbox():用户选择首个按钮时,返回索引值0,否则返回索引值1

>>> indexbox(msg = 'Shall I continue?', title = ' ', choices = ('Yes', 'No'), image = None, default_choice = 'Yes', cancel_choice = 'No')

(6)boolbox():选中首个按钮则返回True,否则返回False

>>> boolbox(msg = 'Shall I continue?', title = ' ', choices = ('[Y]es', '[N]o'), image = None, default_choice = 'Yes', cancel_choice = 'No')

4.buttonbox 中显示图片

>>> buttonbox('输入话语', image = '图像文件名称(可设置 *.gif 和 *.png 格式)', choices = ('选项1', '选项2'))

(图片不显示,why?)

5.用户选项

(1)choicebox():为用户提供可供选择的列表,按照字母排序这些由序列(元组或列表)组成的选项

>>> choicebox(msg = 'Pick an item', title = ' ', choices = [ ], preselect = 0, callback = None, run = True)

(2)multchoicebox():支持用户选择0个、1个或多个选项

>>> choicebox(msg = 'Pick an item', title = ' ', choices = [ ], preselect = 0, callback = None, run = True)

6.用户输入消息

(1)enterbox():提供简单输入框,返回值为用户输入的字符串,默认不保留输入的字符串的首尾空格,如需保留则设置 strip = False

>>> enterbox(msg = 'Enter something', title = ' ', default = ' ', strip = True, image = None, root = None)

(2)integerbox():用户只能输入范围内的整型数值,即(lowerbound, upperbound),否则要求重新输入

# integerbox(msg = ' ', title = ' ', default = None, lowerbound = 0, upperbound = 99, image = None, root = None)>>> import random>>> import easygui as eg>>> eg.msgbox("What do u want from me? I will not tell u the number!")>>> secret = random.randint(1, 10)>>> msg = "Please guess the number i choose from 1 to 10.">>> title = "let's play the game!">>> guess = eg.integerbox(msg, title, lowerbound = 1, upperbound = 10)>>> while True:...if guess == secret:... eg.msgbox("Good job!")... break...else:... if guess > secret:... eg.msgbox("so big")... else:... eg.msgbox("so small")... guess = eg.integerbox(msg, title, lowerbound = 1, upperbound = 10)>>> eg.msgbox("Game Over!")

(3)multenterbox():提供多个简单输入框,且:输入的值少于选项,则返回列表中的值用空字符串填充为用户输入的选项;输入的值多于选项,则返回的值将截断为选项的数量;用户取消操作,则返回域中列表的值或 None 值

# multenterbox(msg = 'Fill in values for the fields ', title = ' ', fields = [ ], values = [ ], callback = None, root = None)>>> import easygui as eg>>> msg = "Show some information about yourself, please!">>> title = "Please take it seriously!">>> fields = ["*Username", "*E-mail", "sex", "*Phone number", "QQ", "WeChat"]>>> field_values = []>>> field_values = eg.multenterbox(msg, title, fields)>>> while 1:...if field_values == None:... break...errmsg = ""...for i in range(len(fields)):... option = fields[i].strip() # 去除字符串的首尾空格...# 若输入的字符串去除首尾空格后为空 且 小标题的字符串首字符为星号则提醒错误信息 ...if field_values[i].strip() == "" and option[0] == "*": ... errmsg += ("【%s】为必填项。\n" % fields[i])...if errmsg == "":... break...# 未填必填项时,提醒错误信息,保留正确填写过的内容...field_values = eg.multenterbox(errmsg, title, fields, field_values)>>> print("That's all: %s" % str(field_values))

7.用户输入密码

(1)passwordbox():同enterbox(),但输入的内容用*号表示

>>> passwordbox(msg = 'Enter your password.', title = ' ', default = ' ', image = None, root = None)

(2)multpasswordbox():同multenterbox(),但最后一个输入框显示为密码的形式(*)

>>> multpasswordbox(msg = '请输入用户名和密码:', title = '登录', fields = ("用户名:", "密码:"))

8.显示文本

(1)textbox():默认以比例字体(参数 codebox = True 设置为等宽字体)来显示文本内容且自动换行

>>> textbox(msg = '', title = '', text = '', codebox = False, callback = None, run = True)# 举例import easygui as egfile = open("XXX.txt")eg.textbox(msg = '文件【XXX.txt】的内容如下:', title = '', text = file.read())

(2)codebox():以等宽字体显示文本内容且不自动换行,相当于 textbox(codebox = True)

>>> codebox(msg = '', title = '', text = '')

9.目录与文件

(1)diropenbox():提供对话框,返回用户选择的目录名(带完整路径),若选择 Cancel 则返回 None,default 用于设置默认的打开目录(需确保设置的目录已存在)

>>> diropenbox(msg = None, title = None, default = None)

(2)fileopenbox():可参考 diropenbox() 函数,但 default 参数的设置不同

>>> fileopenbox(msg = None, title = None, default = '*', filetypes = None, multiple = False)

(3)filesavebox():功能和 filetypes 参数设置可参考 fileopenbox() 函数;default 参数应包含一个文件名(如当前需保存的文件名),也可设置为空或包含一个文件格式掩码的通配符

>>> filesavebox(msg = None, title = None, default = '', filetypes = None)

10.捕获异常

exceptionbox() 函数会将堆栈追踪显示在一个 codebox() 中且允许做进一步处理

# 举例try:print('XXX')int('HaHa')except:exceptionbox()

(如何显示在 codebox() 中?)

11.记住用户设置

即对用户的设置进行存储和恢复

# 存入数据>>> from easygui import EgStore>>> class Settings(EgStore):...def __init__(self, filename):... # 指定要记住的属性名称... self.author = ""... self.book = ""... ... # 必须执行下面两个语句... self.filename = filename... self.restore()>>> settingsFilename = "settings.txt" # 设定存储数据的文本的名字# 创建实例化对象>>> settings = Settings(settingsFilename)>>> author = "小甲鱼真不错">>> book = "《零基础入门学习Python》等等等">>> settings.author = author>>> settings.book = book>>> settings.store()>>> print("\n数据已保存\n")# 取出数据>>> settingsFilename = "settings.txt">>> settings = Settings(settingsFilesname)>>> print(settings.author)

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