如何在 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()輸出
當我們點選按鈕時,將顯示一個訊息框:
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP