python利用easygui打开文件对话框
1.利用msgbox(单词messagebox的缩写)给出一个提示信息:
import easygui as g reply=g.msgbox('This is a basic message box.', 'Title Goes Here') print(reply) #http://easygui.sourceforge.net/
运行结果:
import easygui
easygui.msgbox('Hello, world!')
运行结果:
说明:easygui.msgbox('Hello, world!')这一句有个返回值,就是字符'OK'。这个特点是:只能点击OK,返回值确定。
利用ynbox给出yes or no 对话框:
import easygui as g reply=g.ynbox('Shall I continue?', 'Title', ('Yes', 'No')) print(reply) #http://easygui.sourceforge.net/
运行结果:
说明:点'Yes',返回True,点'No',返回False。
导入easygui时起个别名:
import easygui easygui.msgbox('Hello EasyGui!') #但是,直接使用import导入,之后使用其中的方法时需要加上easygui的前缀,例如easygui.msgbox()。这样比较麻烦,我们还可以选择导入整个EasyGui的包: from easygui import * msgbox('Hello EasyGui!') #上面的方法效果时一样的。我们还有第三种方法: import easygui as g g.msgbox('Hello EasyGui!') #这个方法的好处是保留了EasyGui的命名空间,且调用时不用写出完整的包名。同时避免了第二种方法导致的函数覆盖的可能
#https://www.zybuluo.com/kingwhite/note/128328
2.给出一个提示信息,并且OK按钮的内容可以更改:
from easygui import *
msgbox('您选的序号是未知序号!',ok_button = '关闭程序')
和第一个一样,返回值是字符串,只是出现的返回值的内容可以修改。
3.打开文件对话框:
import easygui path = easygui.fileopenbox()
如果选择打开文件,则返回值是所打开文件的全路径,如果选择取消,则返回'None'。
4.选择多个字符串列表中的某个字符串,并返回显示在对话框上面:
import easygui as g import sys while True: g.msgbox('嗨,欢迎进入第一个GUI制作的小游戏~') msg = '你希望学习到什么知识呢?' title = '互动小游戏' choices = ['琴棋书画', '四书五经', '程序编写', '逆向分析'] choice = g.choicebox(msg, title, choices) # note that we convert the 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 choose Continue else: sys.exit(0) # user choose Cancel #https://i.cnblogs.com/EditPosts.aspx?postid=9914202&update=1
5.和4相似的例子:
import easygui as g import sys while 1: g.msgbox("Hello, world!") msg ="What is your favorite flavor?" title = "Ice Cream Survey" choices = ["Vanilla", "Chocolate", "Strawberry", "Rocky Road"] 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("You chose: " + str(choice), "Survey Result") msg = "Do you want to continue?" title = "Please Confirm" if g.ccbox(msg, title): # show a Continue/Cancel dialog pass # user chose Continue else: sys.exit(0) # user chose Cancel #http://easygui.sourceforge.net/tutorial.html
6.easygui的buttonbox:
import easygui as g reply=g.buttonbox('Click on your favorite flavor.', 'Favorite Flavor', ('Chocolate', 'Vanilla', 'Strawberry')) print(reply) #http://easygui.sourceforge.net/
7.返回选择的文件夹的名字:
import easygui as g reply=g.diropenbox() print(reply)
运行结果:
8.另存为对话框:
import easygui as g reply=g.filesavebox() print(reply)
9.输入内容对话框:
import easygui as g reply=g.enterbox("shuru:") print(reply)