python_tkinter彈出對話框創(chuàng)建
tkinter提供了三個模塊,可以創(chuàng)建彈出對話窗口:(使用必須單獨導(dǎo)入模塊)
1.messagebox消息對話框
示例:askokcancel
import tkinter # 導(dǎo)入消息對話框子模塊 import tkinter.messagebox # 創(chuàng)建主窗口 root = tkinter.Tk() # 設(shè)置窗口大小 root.minsize(300,300) # 聲明函數(shù) def okqqq(): ? ? # 彈出對話框 ? ? result = tkinter.messagebox.askokcancel(title = '標(biāo)題~',message='內(nèi)容:要吃飯嘛?') # 返回值為True或者False ? ? print(result) # 添加按鈕 btn1 = tkinter.Button(root,text = 'ok',command = okqqq) btn1.pack() # 加入消息循環(huán) root.mainloop()
示例:askquestion
import tkinter # 導(dǎo)入消息對話框子模塊 import tkinter.messagebox # 創(chuàng)建主窗口 root = tkinter.Tk() # 設(shè)置窗口大小 root.minsize(300,300) # 聲明函數(shù) def question(): ? ? # 彈出對話框 ? ? result = tkinter.messagebox.askquestion(title = '標(biāo)題',message='內(nèi)容:你吃飯了嘛?') ? ? # 返回值為:yes/no ? ? print(result) # 添加按鈕 btn1 = tkinter.Button(root,text = 'question',command = question) btn1.pack() # 加入消息循環(huán) root.mainloop()
示例:askretrycancel(重試)
import tkinter # 導(dǎo)入消息對話框子模塊 import tkinter.messagebox # 創(chuàng)建主窗口 root = tkinter.Tk() # 設(shè)置窗口大小 root.minsize(300,300) # 聲明函數(shù) def retry(): ? ? # 彈出對話框 ? ? result = tkinter.messagebox.askretrycancel(title = '標(biāo)題',message='內(nèi)容:女生拒絕了你!?') ? ? # 返回值為:True或者False ? ? print(result) # 添加按鈕 btn1 = tkinter.Button(root,text = 'retry',command = retry) btn1.pack() # 加入消息循環(huán) root.mainloop()
示例:askyesno
# 聲明函數(shù) def yesno(): ? ? # 彈出對話框 ? ? result = tkinter.messagebox.askyesno(title = '標(biāo)題',message='內(nèi)容:你喜歡我嗎?') ? ? # 返回值為:True或者False ? ? print(result) # 添加按鈕 btn1 = tkinter.Button(root,text = 'yesno',command = yesno) btn1.pack()
示例:showerror (出錯)
# 聲明函數(shù) def error(): ? ? # 彈出對話框 ? ? result = tkinter.messagebox.showerror(title = '出錯了!',message='內(nèi)容:你的年齡不符合要求。') ? ? # 返回值為:ok ? ? print(result) # 添加按鈕 btn1 = tkinter.Button(root,text = 'error',command = error) btn1.pack()
示例:showwarning(警告)
# 聲明函數(shù) def warning(): ? ? # 彈出對話框 ? ? result = tkinter.messagebox.showwarning(title = '出錯了!',message='內(nèi)容:十八歲以下禁止進入。') ? ? # 返回值為:ok ? ? print(result) # 添加按鈕 btn1 = tkinter.Button(root,text = 'warning',command = warning) btn1.pack()
示例:showinto (信息提示)
# 聲明函數(shù) def info(): ? ? # 彈出對話框 ? ? result = tkinter.messagebox.showinfo(title = '信息提示!',message='內(nèi)容:您的女朋友收到一只不明來歷的口紅!') ? ? # 返回值為:ok ? ? print(result) # 添加按鈕 btn1 = tkinter.Button(root,text = 'info',command = info) btn1.pack()
2.simpledialog 簡單信息對話框
示例:asksting(獲取字符串)
import tkinter # 導(dǎo)入子模塊 import tkinter.simpledialog # 創(chuàng)建主窗口 root = tkinter.Tk() # 設(shè)置窗口大小 root.minsize(300,300) # 創(chuàng)建函數(shù) def askname(): ? ? # 獲取字符串(標(biāo)題,提示,初始值) ? ? result = tkinter.simpledialog.askstring(title = '獲取信息',prompt='請輸入姓名:',initialvalue = '可以設(shè)置初始值') ? ? # 打印內(nèi)容 ? ? print(result) # 添加按鈕 btn = tkinter.Button(root,text = '獲取用戶名',command = askname) btn.pack() # 加入消息循環(huán) root.mainloop()
示例:askinteger(獲取整型)
import tkinter # 導(dǎo)入消息對話框子模塊 import tkinter.simpledialog # 創(chuàng)建主窗口 root = tkinter.Tk() # 設(shè)置窗口大小 root.minsize(300,300) # 創(chuàng)建函數(shù) def askage(): ? ? # 獲取整型(標(biāo)題,提示,初始值) ? ? result = tkinter.simpledialog.askinteger(title = '獲取信息',prompt='請輸入年齡:',initialvalue = '18') ? ? # 打印內(nèi)容 ? ? print(result) # 添加按鈕 btn = tkinter.Button(root,text = '獲取年齡',command = askage) btn.pack() # 加入消息循環(huán) root.mainloop()
示例:askfloat(獲取浮點型)
import tkinter # 導(dǎo)入消息對話框子模塊 import tkinter.simpledialog # 創(chuàng)建主窗口 root = tkinter.Tk() # 設(shè)置窗口大小 root.minsize(300,300) # 創(chuàng)建函數(shù) def askheight(): ? ? # 獲取浮點型數(shù)據(jù)(標(biāo)題,提示,初始值) ? ? result = tkinter.simpledialog.askfloat(title = '獲取信息',prompt='請輸入身高(單位:米):',initialvalue = '18.0') ? ? # 打印內(nèi)容 ? ? print(result) # 添加按鈕 btn = tkinter.Button(root,text = '獲取身高',command = askheight) btn.pack() # 加入消息循環(huán) root.mainloop()
到此這篇關(guān)于python_tkinter彈出對話框創(chuàng)建的文章就介紹到這了,更多相關(guān)tkinter對話框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法
這篇文章主要介紹了Python實現(xiàn)登錄人人網(wǎng)并抓取新鮮事的方法,可實現(xiàn)Python模擬登陸并抓取新鮮事的功能,需要的朋友可以參考下2015-05-05Pandas中DataFrame基本函數(shù)整理(小結(jié))
這篇文章主要介紹了Pandas中DataFrame基本函數(shù)整理(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Keras構(gòu)建神經(jīng)網(wǎng)絡(luò)踩坑(解決model.predict預(yù)測值全為0.0的問題)
這篇文章主要介紹了Keras構(gòu)建神經(jīng)網(wǎng)絡(luò)踩坑(解決model.predict預(yù)測值全為0.0的問題),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python初識二叉樹續(xù)之實戰(zhàn)binarytree
binarytree庫是一個Python的第三方庫,這個庫實現(xiàn)了一些二叉樹相關(guān)的常用方法,使用二叉樹時,可以直接調(diào)用,不需要再自己實現(xiàn),下面這篇文章主要給大家介紹了關(guān)于Python初識二叉樹之實戰(zhàn)binarytree的相關(guān)資料,需要的朋友可以參考下2022-05-05python 實現(xiàn)mysql自動增刪分區(qū)的方法
這篇文章主要介紹了python 實現(xiàn)mysql自動增刪分區(qū)的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04