導入easygui模塊有很多種方法 , 這里只介紹一種簡單使用的 .
1 import easygui as g
將easygui 簡稱為g 然后開始調用她的函數就行.
1 import easygui as g 2 import sys 3 while 1: 4 g.msgbox("顯示一個窗口並且顯示這些文字")# 只顯示一個對話框並且只有一個ok 5 msg="你希望學到什么呢?" 6 title="小游戲互動" # 在左上角的 標題曠里面 7 choices=['談戀愛','編程','ooxx','琴棋書畫'] # 在選擇框內 , 提供可選擇項 8 choice=g.choicebox(msg,title,choices) # 在這里 choice 可以得到上面你選擇的那個選項 9 g.msgbox("你的選擇是:"+str(choice),'結果') # 打印出來 10 msg='你希望再來一次么?' 11 title='請選擇' 12 if g.ccbox(msg,title): # ok為真 cancel為假 13 pass 14 else: 15 exit(0) # 用於退出程序 .
1 >>> import easygui as g 2 >>> g.msgbox('我愛博主','人民心聲') 3 'OK'
在函數中有許多的默認參數 如下
1 import easygui as g 2 import sys 3 choices=['願意','不願意','聽從您的吩咐'] 4 reply=g.choicebox('你願意和我在一起么,美女.',choices=choices) 5 g.msgbox(reply)
msgbox的函數定義如下
1 >>> help(g.msgbox) 2 Help on function msgbox in module easygui: 3 4 msgbox(msg='(Your message goes here)', title=' ', ok_button='OK', image=None, root=None) 5 Display a messagebox
這里示范一下修改按鈕的辦法 .
1 g.msgbox('are you ready?',ok_button='呦我草')
關於ccbox
1 msgbox(msg='(Your message goes here)', title=' ', ok_button='OK', image=None, root=None)
1 if g.ccbox('要再來一次嗎?', choices=('要啊要啊^_^', '算了吧T_T')): 2 g.msgbox('不給玩了,再玩就玩壞了......') 3 else: 4 sys.exit(0) # 記得先 import sys 哈
關於buttonbox
1 buttonbox(msg='', title=' ', choices=('Button1', 'Button2', 'Button3'), image=None, root=None)
1 g.msgbox(g.buttonbox("我愛你",'你說愛不愛',('唉','你唉','愛愛愛','愛愛愛')))
如何添加圖片.
1 import easygui as g 2 import sys 3 g.buttonbox('大家說我長得帥嗎?', image='D:/Documents/Pictures/2.gif', choices=('帥', '不帥', '!@#$%'))
為用戶提供一組選項.
1 multchoicebox(msg='Pick as many items as you like.', title=' ', choices=(), **kwargs) 2 Present the user with a list of choices. 3 allow him to select multiple items and return them in a list. 4 if the user doesn't choose anything from the list, return the empty list. 5 return None if he cancelled selection.
1 import easygui as g 2 str1=g.multchoicebox('你現在最想干啥','這是標題',('抽煙','打游戲','和蔡偉偉一起玩','學東西寫代碼','唉,日了狗了')) 3 if str1==None: 4 g.msgbox('如果點擊取消的話 會返回一個 None') 5 print(str1) 6 g.msgbox(str1)
讓用戶輸入
1 enterbox(msg='Enter something.', title=' ', default='', strip=True, image=None, root=None) 2 Show a box in which a user can enter some text. 3 4 You may optionally specify some default text, which will appear in the 5 enterbox when it is displayed. 6 7 Returns the text that the user entered, or None if he cancels the operation. 8 9 By default, enterbox strips its result (i.e. removes leading and trailing 10 whitespace). (If you want it not to strip, use keyword argument: strip=False.) 11 This makes it easier to test the results of the call:: 12 13 reply = enterbox(....) 14 if reply: 15 ... 16 else:
1 >>> str1=g.enterbox(msg='請輸入你想說的話', title='交互界面 ', default='這是默認用戶輸入', strip=True, image='D:/Documents/Pictures/2.gif', root=None)
讓用戶輸入數據
integerbox(msg='', title=' ', default='', lowerbound=0, upperbound=99, image=None, root=None, **invalidKeywordArguments)
1 >>> g.integerbox(msg='輸入你的分數 0~150', title='分數采集 ', default=0, lowerbound=0, upperbound=150) 2 150
顯示文本文檔.
1 import easygui as g 2 f=open('C:/Users/Administrator/Desktop/新建文本文檔.txt') 3 str1=f.read() 4 g.textbox('下面是題解','求模',str1,1)
輸入帳號密碼.
1 passwordbox(msg='Enter your password.', title=' ', default='', image=None, root=None)
g.msgbox(g.passwordbox('輸入你的密碼','登陸提示','123456789'))
帳號密碼登錄框
1 multpasswordbox(msg='Fill in values for the fields.', title=' ', fields=(), values=()) 2 Same interface as multenterbox. But in multpassword box, 3 the last of the fields is assumed to be a password, and 4 is masked with asterisks.
import easygui as g (account,password)=g.multpasswordbox('請輸入您的賬號密碼','登錄框',('帳號','密碼')) g.msgbox('帳號: '+account+'\n'+'密碼: '+password)
目錄和文件
1 import easygui as g 2 #gui編程中常見的一個場景是要求用戶輸入目錄或者文件名 3 #easygui提供了一些基本的函數讓用戶來瀏覽文件系統,選擇一個目錄或者文件. 4 str1=g.diropenbox('選擇文件目錄','瀏覽文件夾','C:/Users/Administrator/Desktop') 5 g.msgbox(str1)
選擇一個文件並且返回包括文件名的目錄
1 fileopenbox(msg=None, title=None, default='*', filetypes=None)
1 import easygui as g 2 #fileopenbox()函數用於提供一個對話框 , 返回用戶選擇的文件名(帶完整路徑) 3 #如果用戶選擇 Cancel則返回None. 4 str1=g.fileopenbox('選擇文件','提示','C:/Users/Administrator/Desktop/__pycache__') 5 g.msgbox(str1)