Python 模塊EasyGui詳細介紹
前言:
在Windows想用Python開發一些簡單的界面,所以找到了很容易上手的EasyGui庫。下面就分享一下簡單的使用吧。
參考的鏈接:官網Tutorial
接下來,我將從簡單,到復雜一點點的演示如何使用這個模塊。希望能給剛接觸easygui的你一點幫助 :-)
msgBox,ccbox,ynbox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 一個最簡單的類似于Java的MessageBox的小窗口 import easygui title = easygui.msgbox(msg='提示信息',title='標題部分',ok_button="OOK") msg = easygui.msgbox('Hello Easy GUI') print '返回值:' + msg ccbox = easygui.ccbox("here is Continue | Cancel Box!") print '返回值:' + str(ccbox) ynbox = easygui.ynbox("Yes Or No Button Box!") print '返回值: ' + str(ynbox)
bottonbox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 能讓你最初選擇的簡單的界面,第二個參數為一個列表 import easygui # choice = easygui.buttonbox("這里是提示的語句信息:\n", title='三選一', choices=['one' \ # , 'two', 'three']) # easygui.msgbox('您選擇了:' + str(choice)) # # # choices 內只能有兩個參數 ,選擇哪一個將返回1,否則返回0 # bool = easygui.boolbox('msg提示信息', title='標題部分', choices=['A', 'B']) # easygui.msgbox(bool) image = 'me.jpg' msg = 'Here is my photo,a python fan also' choices = ['Yes','No',"Not Sure"] title = 'Am I handsome?' easygui.buttonbox(msg,title,image=image,choices=choices)
choicebox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 從一個列表中選擇其中的一個,會有返回值的出現 import easygui msg = '選擇此列表項中你喜歡的一個吧' title = '必須選擇一個哦' choices = ['1','2','3','4','5','6','7'] answer = easygui.choicebox(msg,title,choices) print '你選擇了 :' + str(answer)
enterbox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 可以滿足用戶輸入的控件 import easygui st = easygui.enterbox("請輸入一段文字:\n") print "您輸入了: " + str(st)
mutilchoicebox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 一個多選的列表項.呵呵了,這個版本貌似有問題。我的多選并沒有真正的實現 import easygui msg = '選擇此列表項中你喜歡的一個吧' title = '必須選擇一個哦' choices = (1,2,3,4,5,6,7,8,9) answer1 = easygui.multchoicebox(msg,title,choices) for item in answer1: print item
intenterbox,passenterbox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 提供給用戶簡單的輸入框,只能是給定的數字的范圍 import easygui msg = '請輸入一個數字,范圍在0-100' title = '限制為數字類型' lowerbound = 0 upperbound = 100 default = '' image = 'me.jpg' result = easygui.integerbox(msg,title,default,lowerbound,upperbound,image) print result
textbox,codebox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = easygui 還提供了對大量文本的支持,以及對代碼文本的支持 import easygui msg = '大文本的支持' title = 'Text Code' text = 'abcdefghijklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVWXYZ0123456789-/' textContent = easygui.textbox(msg,title,text) codeContent = easygui.codebox(msg,title,) print textContent print codeContent # D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/text_codebox.py # abcdefghijklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVWXYZ0123456789-/ # public class HelloWorld{ # public static void main(String []args) { # System.out.println("Hello World!"); # } # } # # Process finished with exit code 0
diropenbox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 該函數用于提供一個對話框,返回用戶選擇的目錄名,該目錄名是帶有完整的路徑的 # 選擇Cancel的話返回值默認為None import easygui msg = '選擇一個文件,將會返回該文件的完整的目錄哦' title = ' 文件選擇對話框' default = r'F:\flappy-bird' full_file_path = easygui.diropenbox(msg, title, default) print '選擇的文件的完整的路徑為:' + str(full_file_path) # D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/diropenbox.py # 選擇的文件的完整的路徑為:F:\flappy-bird # # Process finished with exit code 0
fileopenbox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 此方法用于提供一個對話框,返回用戶選擇的文件名,帶有完整的路徑,選擇Cancel返回None # default="c:/fishc/*.py" 即顯示 C:\fishc 文件夾下所有的 Python 文件。 # default="c:/fishc/test*.py" 即顯示 C:\fishc 文件夾下所有的名字以 test 開頭的 Python 文件。 # filetypes參數是包含文件掩碼的字符串的列表,記住是個列表。如:filetypes = ["*.css", ["*.htm", "*.html", "HTML files"]] import easygui msg = '返回選擇的文件的完整的路徑,選擇Cancel則返回None' title = '文件選擇器' default = 'E:/Code/Python/MyTestSet/easygui/*.py' opened_files = easygui.fileopenbox(msg,title,default,multiple=True) for item in opened_files: print item # D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/fileopenbox.py # E:\Code\Python\MyTestSet\easygui_\me.jpg # E:\Code\Python\MyTestSet\easygui_\buttonbox.py # E:\Code\Python\MyTestSet\easygui_\choicesbox.py # E:\Code\Python\MyTestSet\easygui_\diropenbox.py # E:\Code\Python\MyTestSet\easygui_\enterbox.py # E:\Code\Python\MyTestSet\easygui_\fileopenbox.py # E:\Code\Python\MyTestSet\easygui_\integerbox.py # # Process finished with exit code 0
filesavebox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 該函數提供了一個對話框,讓用戶選擇文件需要保存的路徑(帶完整的路徑)選擇Cancel返回None # default 參數應該包含一個文件名(例如當前需要保存的文件名),當然你也可以設置為空的,或者包含一個文件格式掩碼的通配符。 # filetypes參考如上面的fileopenbox import easygui msg = 'Save your file' title = "to Save File" default = 'E:/Code/Python/MyTestSet/easygui/newFile.*' savedfile = easygui.filesavebox(msg,title,default) print savedfile print '當然了,這里僅僅是一個完整的路徑加上文件名而已,并不會真的保存成一個文件,保存文件需要用到其他的庫' # D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/easygui_/filesavebox.py # E:\Code\Python\MyTestSet\easygui_\newFile.doc # 當然了,這里僅僅是一個完整的路徑加上文件名而已,并不會真的保存成一個文件,保存文件需要用到其他的庫 # # Process finished with exit code 0
exceptionbox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 這是一個很好用的對話框,當應用程序出現異常的時候,就可以通過這個來給與用戶友好的界面提示 import easygui try: int('Exception') except: easygui.exceptionbox('int類型數據轉換錯誤!請檢查您的數據類型!') # 會彈出一個界面,內容信息可以自己定義,如上面。下面的內容就是追蹤到的出錯信息 # Traceback (most recent call last): # File "E:/Code/Python/MyTestSet/easygui_/exceptionbox.py", line 10, in# int('Exception') # ValueError: invalid literal for int() with base 10: 'Exception'
總結
看完了這些示例,想必對easygui開發簡單的桌面小程序很有信心了吧。(^__^) 嘻嘻……
但是咧,對于比較復雜的任務,只是掌握了這些基礎的是遠遠不夠的。所以我們還需要挖掘一下Python其他的相關的模塊。這樣在實際開發的時候,就可以根據任務的難易程度選擇最合適的模塊進行開發了。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
