【问题标题】:Tkinter entry widget .get doesn't workTkinter 条目小部件 .get 不起作用
【发布时间】:2016-11-06 11:21:18
【问题描述】:

我是 python 的新手,目前正在做一个学校项目,我的目标是创建一个可用于搜索数据文件的搜索栏,但是我正在努力让搜索栏正常工作。我正在使用 tkinter 条目小部件。 当我调用 .get() 时,不会打印条目小部件中的字符串。这是我的代码...

from tkinter import *

def searchButton():
        text = searched.get()
        print (text)

def drawStatWindow():
    global searched
    statWindow = Tk()
    statWindow.title("View Statistics")
    statWindow.config(bg = "grey")
    statWindow.geometry('800x900')

    searched = StringVar()
    searchBox = Entry(statWindow, textvariable = searched)
    searchBox.place(x= 450, y=50, width = 200, height = 24)
    enterButton = tkinter.Button(statWindow, text ="Enter", command =searchButton)
    enterButton.config(height = 1, width = 4)
    enterButton.place(x=652, y=50)

drawStatWindow()

当我在条目小部件中键入一个字符串并按下回车按钮时,没有任何反应。 就像我说我不是很有经验,这是我的第一个项目,但是在阅读了 tkinter 条目小部件之后,我不明白为什么这不起作用。 我正在使用 python V3.4.0 谢谢。

【问题讨论】:

  • 您问题中的代码不会运行。请花时间发布说明问题的实际代码。

标签: python tkinter widget


【解决方案1】:

您的代码缺少对mainloop() 的调用。您可以尝试将其添加到 drawStatWindow() 函数的末尾:

statWindow.mainloop()

您可能希望将代码重组为一个类。这使您可以避免使用全局变量,并且通常可以为您的应用程序提供更好的组织:

from tkinter import *

class App:
    def __init__(self, statWindow):
        statWindow.title("View Statistics")
        statWindow.config(bg = "grey")
        statWindow.geometry('800x900')

        self.searched = StringVar()
        searchBox = Entry(statWindow, textvariable=self.searched)
        searchBox.place(x= 450, y=50, width = 200, height = 24)
        enterButton = Button(statWindow, text ="Enter", command=self.searchButton)
        enterButton.config(height = 1, width = 4)
        enterButton.place(x=652, y=50)

    def searchButton(self):
        text = self.searched.get()
        print(text)


root = Tk()
app = App(root)
root.mainloop()

【讨论】:

  • 非常感谢,我一直想像这样重组我的代码,但是我以前从未使用过类,所以这给了我一个很好的起点。我将对此进行更多研究,并尝试像这样重组我的整个程序。
【解决方案2】:

您必须添加mainloop(),因为tkinter 需要它来运行。

如果您在 IDLE 中运行使用 tkinter 的代码,则 IDLE 运行自己的 mainloop() 并且代码可以工作,但通常您必须在末尾添加 mainloop()

你必须在tkinter.Button中删除tkinter

from tkinter import *

def searchButton():
    text = searched.get()
    print(text)

def drawStatWindow():
    global searched

    statWindow = Tk()
    statWindow.title("View Statistics")
    statWindow.config(bg="grey")
    statWindow.geometry('800x900')

    searched = StringVar()

    searchBox = Entry(statWindow, textvariable=searched)
    searchBox.place(x= 450, y=50, width=200, height=24)

    # remove `tkinter` in `tkinter.Button`
    enterButton = Button(statWindow, text="Enter", command=searchButton)
    enterButton.config(height=1, width=4)
    enterButton.place(x=652, y=50)

    # add `mainloop()`
    statWindow.mainloop()

drawStatWindow()

【讨论】:

    【解决方案3】:

    不需要使用文本变量,你应该使用这个:

    searchBox = Entry(statWindow)
    searchBox.focus_set()
    searchBox.place(x= 450, y=50, width = 200, height = 24)
    

    然后你就可以使用searchBox.get()了,那将是一个字符串。

    【讨论】:

    • 谢谢这工作正常!只是出于兴趣,focus_set() 在做什么?
    • 它返回条目中写的字符串,我不知道比那个更好。
    • 不,focus_set() 确实 not “返回写入条目中的字符串”。顾名思义,它只是将焦点设置在条目中。对searchBox.get()的结果没有影响。
    • 是的,它有效。我说的错误是它返回一个字符串。您将收到一个带有 get() 的字符串
    猜你喜欢
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多