如何在 Tkinter 中建立訊息框?


在某個應用程式中,我們可以使用訊息框方法建立訊息框。以下是我們可以為某個應用程式建立的訊息框的列表:

  • showinfo() - 在螢幕上顯示一般訊息。

  • showwarning() - 向用戶顯示警告。

  • showerror() - 顯示錯誤訊息。

  • askquestion() - 透過訊息框詢問使用者。

  • asktocancel() - 顯示取消操作的資訊。

  • askretrycancel() - 顯示提示使用者是否重試的訊息。

示例

在這個示例中,我們將建立一個應用程式,點選按鈕後將顯示一個資訊訊息框。

#Import required libraries
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

#Create an instance of tkinter frame
win= Tk()

#Define the geometry of the window
win.geometry("750x250")

#Define a function to show the messagebox
def handler():
   messagebox.showinfo("Message", "You have clicked a Button")

#Create a Label
Label(win, text= "Click the below button", font=('Helvetica 16
underline')).pack(pady=20)

#Create a Button
ttk.Button(win, text= "Click", command= handler).pack(pady=30)
win.mainloop()

輸出

當我們點選按鈕時,將顯示一個訊息框:


更新於: 2021 年 5 月 4 日

3000 多次瀏覽

開啟你的 職業

完成課程,成為認證人員

開始
廣告
© . All rights reserved.