本博文元基於python的tkinter.messagebox模塊中的askocancel、askquestion、askyesno、showerror、showinfo、showwarning建立簡單的消息框。如果彈出需要傳遞title於message參數。
實驗效果
實驗原理
首先創建按鈕消息處理函數,然後將其綁定到按鈕與主窗口中。開始點擊然後依次彈出。
實驗代碼
# -*- coding:utf-8 -*-
import tkinter
import tkinter.messagebox
def cmd():
global n
global buttontext
n += 1
if n == 1:
tkinter.messagebox.askokcancel('我是消息框','askokcancel')
buttontext.set('skquestion')
elif n == 2:
tkinter.messagebox.askquestion('我是消息框', 'askquestion')
buttontext.set('askyesno')
elif n == 3:
tkinter.messagebox.askyesno('我是消息框', 'askyesno')
buttontext.set('showerror')
elif n == 4:
tkinter.messagebox.showerror('我是消息框', 'showerror')
buttontext.set('showinfo')
elif n == 5:
tkinter.messagebox.showinfo('我是消息框', 'showinfo')
buttontext.set('showwarnning')
else:
n == 0
tkinter.messagebox.showwarning('我是消息框', 'showwarning')
buttontext.set('askokcancel')
if __name__ == '__main__':
n = 0
root = tkinter.Tk()
buttontext = tkinter.StringVar()
buttontext.set('askokcancel')
button = tkinter.Button(root,textvariable=buttontext,command = cmd)
button.pack()
root.mainloop()