Python之tkinter.messagebox弹窗

messagebox:tkinter的消息框、对话框

 

一、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()

效果:

Python之tkinter.messagebox弹窗_第1张图片

 

 

 

 二、tkinter.messagebox.showwarning(title,message)

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()

 

效果:

Python之tkinter.messagebox弹窗_第2张图片

 

 

 

 、tkinter.messagebox.showinfo(title,message)

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()

 

效果:

Python之tkinter.messagebox弹窗_第3张图片

 

 

 

 四、messagebox.askquestion(title='提示',message='错误') #resurn 'yes' or 'no'

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)

Python之tkinter.messagebox弹窗_第4张图片

 

 

 

 五、messagebox.askyesno(title='提示', message='错误') # resurn 'True' or 'False'

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

效果:(说明:按下是或否会返回True或False

Python之tkinter.messagebox弹窗_第5张图片

 

 六、messagebox.askretrycancel(title='提示', message='错误') # resurn 'True' or 'False'

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

效果:说明:按下是或否会返回True或False

Python之tkinter.messagebox弹窗_第6张图片

 

 七、messagebox.askokcancel(title='提示', message='错误') # resurn 'True' or 'False'

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

效果:说明:按下是或否会返回True或False

Python之tkinter.messagebox弹窗_第7张图片

 

 

 

 

你可能感兴趣的:(Python之tkinter.messagebox弹窗)