前言:
在Windows想用Python開發(fā)一些簡單的界面,所以找到了很容易上手的EasyGui庫。下面就分享一下簡單的使用吧。
接下來,我將從簡單,到復雜一點點的演示如何使用這個模塊。希望能給剛接觸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__ = 能讓你最初選擇的簡單的界面,第二個參數(shù)為一個列表 import easygui # choice = easygui.buttonbox("這里是提示的語句信息: ", title='三選一', choices=['one' # , 'two', 'three']) # easygui.msgbox('您選擇了:' + str(choice)) # # # choices 內(nèi)只能有兩個參數(shù) ,選擇哪一個將返回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__ = 從一個列表中選擇其中的一個,會有返回值的出現(xiàn) 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("請輸入一段文字: ") print "您輸入了: " + str(st)
mutilchoicebox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 一個多選的列表項.呵呵了,這個版本貌似有問題。我的多選并沒有真正的實現(xiàn) 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__ = 提供給用戶簡單的輸入框,只能是給定的數(shù)字的范圍 import easygui msg = '請輸入一個數(shù)字,范圍在0-100' title = '限制為數(shù)字類型' 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:SoftwarePython2python.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__ = 該函數(shù)用于提供一個對話框,返回用戶選擇的目錄名,該目錄名是帶有完整的路徑的 # 選擇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:SoftwarePython2python.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參數(shù)是包含文件掩碼的字符串的列表,記住是個列表。如: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:SoftwarePython2python.exe E:/Code/Python/MyTestSet/easygui_/fileopenbox.py # E:CodePythonMyTestSeteasygui_me.jpg # E:CodePythonMyTestSeteasygui_uttonbox.py # E:CodePythonMyTestSeteasygui_choicesbox.py # E:CodePythonMyTestSeteasygui_diropenbox.py # E:CodePythonMyTestSeteasygui_enterbox.py # E:CodePythonMyTestSeteasygui_fileopenbox.py # E:CodePythonMyTestSeteasygui_integerbox.py # # Process finished with exit code 0
filesavebox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 該函數(shù)提供了一個對話框,讓用戶選擇文件需要保存的路徑(帶完整的路徑)選擇Cancel返回None # default 參數(shù)應該包含一個文件名(例如當前需要保存的文件名),當然你也可以設置為空的,或者包含一個文件格式掩碼的通配符。 # 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:SoftwarePython2python.exe E:/Code/Python/MyTestSet/easygui_/filesavebox.py # E:CodePythonMyTestSeteasygui_ ewFile.doc # 當然了,這里僅僅是一個完整的路徑加上文件名而已,并不會真的保存成一個文件,保存文件需要用到其他的庫 # # Process finished with exit code 0
exceptionbox
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/25' # __Desc__ = 這是一個很好用的對話框,當應用程序出現(xiàn)異常的時候,就可以通過這個來給與用戶友好的界面提示 import easygui try: int('Exception') except: easygui.exceptionbox('int類型數(shù)據(jù)轉(zhuǎn)換錯誤!請檢查您的數(shù)據(jù)類型!') # 會彈出一個界面,內(nèi)容信息可以自己定義,如上面。下面的內(nèi)容就是追蹤到的出錯信息 # Traceback (most recent call last): # File "E:/Code/Python/MyTestSet/easygui_/exceptionbox.py", line 10, in <module> # int('Exception') # ValueError: invalid literal for int() with base 10: 'Exception'
總結(jié)
看完了這些示例,想必對easygui開發(fā)簡單的桌面小程序很有信心了吧。(^__^) 嘻嘻……
但是咧,對于比較復雜的任務,只是掌握了這些基礎的是遠遠不夠的。所以我們還需要挖掘一下Python其他的相關的模塊。這樣在實際開發(fā)的時候,就可以根據(jù)任務的難易程度選擇最合適的模塊進行開發(fā)了。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
更多Python 模塊EasyGui詳細介紹相關文章請關注PHP中文網(wǎng)!
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com