Python之tkinter.messagebox彈窗

messagebox:tkinter的消息框、對話框oop

 

1、messagebox.showinfo(title='提示', message='錯誤')佈局

 1 from tkinter import *
 2 from tkinter import messagebox
 3 root = Tk()# 初始化
 4 
 5 width = 380
 6 height = 300
 7 # 獲取屏幕尺寸以計算佈局參數,使窗口居屏幕中央
 8 screenwidth = root.winfo_screenwidth()
 9 screenheight = root.winfo_screenheight()
10 alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
11 root.geometry(alignstr)
12 frame = Frame(root)
13 frame.pack()
14 def hit_me():
15     messagebox.showinfo(title='提示', message='錯誤')
16 button = Button(frame, text='點我', command=hit_me) 17 button.pack() 18 mainloop()

效果:spa

 

 

 

 2、tkinter.messagebox.showwarning(title,message)3d

from tkinter import *
from tkinter import messagebox
root = Tk()
width = 380
height = 300
# 獲取屏幕尺寸以計算佈局參數,使窗口居屏幕中央
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
frame = Frame(root)
frame.pack()
def hit_me():
    # messagebox.showinfo(title='提示', message='錯誤')
    messagebox.showwarning(title='提示', message='錯誤')
button = Button(frame, text='點我', command=hit_me)
button.pack()
mainloop()

 

效果:code

 

 

 

 、tkinter.messagebox.showinfo(title,message)blog

from tkinter import *
from tkinter import messagebox
root = Tk()
width = 380
height = 300
# 獲取屏幕尺寸以計算佈局參數,使窗口居屏幕中央
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
frame = Frame(root)
frame.pack()
def hit_me():
    # messagebox.showinfo(title='提示', message='錯誤')
    # messagebox.showwarning(title='提示', message='錯誤')
    messagebox.showerror(title='提示',message='錯誤')
button = Button(frame, text='點我', command=hit_me)
button.pack()
mainloop()

 

效果:it

 

 

 

 4、messagebox.askquestion(title='提示',message='錯誤') #resurn 'yes' or 'no'io

from tkinter import *
from tkinter import messagebox
root = Tk()
width = 380
height = 300
# 獲取屏幕尺寸以計算佈局參數,使窗口居屏幕中央
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
frame = Frame(root)
frame.pack()
def hit_me():
    # messagebox.showinfo(title='提示', message='錯誤')
    # messagebox.showwarning(title='提示', message='錯誤')
    # messagebox.showerror(title='提示',message='錯誤')
    Q = messagebox.askquestion(title='提示',message='錯誤') #resurn 'yes' or 'no'
    print(Q)
button = Button(frame, text='點我', command=hit_me)
button.pack()
mainloop()

效果:(說明:按下是或否會返回yes或no)class

 

 

 

 5、messagebox.askyesno(title='提示', message='錯誤') # resurn 'True' or 'False'import

Q = messagebox.askyesno(title='提示', message='錯誤')  # resurn 'True' or 'False'

效果:(說明:按下是或否會返回True或False

 

 6、messagebox.askretrycancel(title='提示', message='錯誤') # resurn 'True' or 'False'

Q = messagebox.askretrycancel(title='提示', message='錯誤')  # resurn 'True' or 'False'

效果:說明:按下是或否會返回True或False

 

 7、messagebox.askokcancel(title='提示', message='錯誤') # resurn 'True' or 'False'

Q = messagebox.askokcancel(title='提示', message='錯誤')  # resurn 'True' or 'False'

效果:說明:按下是或否會返回True或False

相關文章
相關標籤/搜索