简体   繁体   中英

Tkinter entry widget .get doesn't work

I am fairly new to python and am currently working on a school project, my aim is to create a search bar that can be used to search a data file, however I am struggling to get the search bar to work correctly. I am using the tkinter entry widget. When I call .get(), the string in the entry widget is not printed. Here is my code...

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()

When I type a string into the entry widget and press the enter button, nothing happens. Like I say I am not very experienced and this is my first project, but after reading about the tkinter entry widgets I can't understand why this won't work. I am using python V3.4.0 Thanks.

Your code lacks a call to mainloop() . You could try adding it to the end of the drawStatWindow() function:

statWindow.mainloop()

You might want to restructure your code into a class. This allows you to avoid using global variables and generally provides better organisation for your application:

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()

You have to add mainloop() because tkinter needs it to run.

If you run code in IDLE which use tkinter then IDLE runs own mainloop() and code can work but normally you have to add mainloop() at the end.

And you have to remove tkinter in tkinter.Button .

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()

No need to use textvariable, you should use this:

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

then you will be able to use searchBox.get(), that will be a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM