Tkinter(十):Messagebox 彈窗


 

效果:點擊hit me后彈出對應的messagebox,有的messagebox會有返回值,比如Yes/No,True/False。

然后通過返回值,進行對應的判斷操作

 

import tkinter as tk
import tkinter.messagebox  # python3需要引入這個模塊,不然會報錯

# 定義窗口
window = tk.Tk()
window.title('my window')  # 窗口title
window.geometry('200x200')  # 窗口尺寸


def hit_me():
    # tk.messagebox.showinfo(title='Hi', message='hahahaha')
    # tk.messagebox.showwarning(title='Hi', message='It is warning')
    # tk.messagebox.showerror(title='error', message='It is error')
    # tk.messagebox.askquestion(title='askquestion', message='It is askquestion')  # return Yes or No,可通過返回值進行判斷操作
    # tk.messagebox.askyesno(title='askyesno', message='It is askyesno')  # return True or False,可通過返回值進行判斷操作
    # tk.messagebox.askretrycancel(title='askretrycancel',
    #                              message='It is askretrycancel')  # return True or False,可通過返回值進行判斷操作
    tk.messagebox.askokcancel(title='askokcancel', message='It is askokcancel')  # return True or False,可通過返回值進行判斷操作


tk.Button(window, text='hit me', command=hit_me).pack()

window.mainloop()

總結:

1.根據需求選擇對應的messagebox

2.python3需要額外引入 tkinter.messagebox 模塊,不然會報錯


免責聲明!

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



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