Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Các loại thông báo (Message Box) trong Tkinter Python


Tkinter là module GUI của Python. Dùng các tùy chọn hiển thị thông báo phản hồi hành động người dùng hoặc thay đổi trạng thái. Lớp messagebox hiển thị xác nhận, lỗi, cảnh báo...

Ví dụ 1: Message tùy chỉnh

import tkinter as tk
main = tk.Tk()
key = "the key to success is to focus on goals and not on obstacles"
message = tk.Message(main, text=key)
message.config(bg='white', font=('times', 32, 'italic'))
message.pack()
tk.mainloop()

Kết quả hình ảnh hiển thị thông báo tùy chỉnh.

Các loại thông báo (Message Box) trong Tkinter Python

Ví dụ 2: Các hộp thoại chuẩn

messagebox có nhiều hàm cho loại thông báo khác nhau.

Hộp hỏi (Question Box)

Dùng askquestion().

from tkinter.messagebox import *
print(askquestion("Question", "Proceed to next Step?"))

Các loại thông báo (Message Box) trong Tkinter Python

Hộp thử lại (Retry Box)

Dùng askretrycancel().

from tkinter.messagebox import *
print(askretrycancel("Retry", "Try Again?"))

Các loại thông báo (Message Box) trong Tkinter Python

Hộp lỗi (Error Box)

Dùng showerror().

from tkinter.messagebox import *
print(showerror("Error", "Error in checkout"))

Các loại thông báo (Message Box) trong Tkinter Python

Hộp cảnh báo (Warning Box)

Dùng showwarning().

from tkinter.messagebox import *
print(showwarning("Warning", "This may result in delay !"))

Các loại thông báo (Message Box) trong Tkinter Python