簡體   English   中英

獲取Tkinter Entry小部件的內容

[英]Get contents of a Tkinter Entry widget

我正在創建一個應用程序,我想在GUI Entry小部件中使用輸入的值。

如何從Tkinter Entry小部件獲取輸入的輸入?

root = Tk()
...
entry = Entry(root)
entry.pack()

root.mainloop()

您需要做兩件事:保持對窗口小部件的引用,然后使用get()方法獲取字符串。

這是一個例子:

self.entry = Entry(...)
...
print("the text is", self.entry.get())

這是一個例子:

import tkinter as tk

class SampleApp(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.pack()
        self.entry.pack()

    def on_button(self):
        print(self.entry.get())

w = SampleApp()
w.mainloop()

首先聲明一個所需類型的變量。 例如一個整數:

var = IntVar()

然后:

entry = Entry(root, textvariable = var

entry.pack()

user_input = var.get()

root.mainloop()

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM