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

效果:

 

 

 

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

 

效果:

 

 

 

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

 

效果:

 

 

 

 四、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)

 

 

 

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

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

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

 

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

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

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

 

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

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

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

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM