如何在 Tkinter 中在訊息框中獲取輸入框?


Tkinter 中的 messagebox 庫提供了多種方法和內建函式。假設你想顯示一個訊息框,並在輸入視窗小程式中從使用者獲取一些輸入。在這種情況下,可以使用 simpledialog 中的 askstring 庫。askstring 庫建立一個視窗,它使用兩個引數,視窗標題和輸入視窗小程式之前的輸入標題。我們舉個例子來看看它是如何工作的。

示例

# Import the required library
from tkinter import *
from tkinter.simpledialog import askstring
from tkinter.messagebox import showinfo

# Create an instance of tkinter frame and window
win=Tk()
win.geometry("700x300")

name = askstring('Name', 'What is your name?')
showinfo('Hello!', 'Hi, {}'.format(name))

win.mainloop()

輸出

執行上面的程式碼會顯示一個彈出訊息框,要求使用者在給定的輸入視窗小程式中輸入姓名。

輸入姓名並單擊“確定”。它將顯示以下訊息−

更新於: 2021-12-16

6 千 + 瀏覽量

開啟您的職業

完成該課程取得認證

開始
廣告