Get text entry tkinter

0

I want the text that I enter in the window entry, after clicking on start, I will return it in this variable:

 d = (Inventory_data.get ("Inventory_data")). get ("texto introducido")

Code:

ventana = Tk()
ventana.geometry("500x300+100+100")
ventana.title("MijaGato")
Tag_Name = Label(text="Name Of Tag").place(x=200, y=100)
entrada = StringVar()
textUsuario = Entry(ventana,textvariable=entrada,width=30).place(x=150, y=140)
button = Button(ventana, text="Start").place(x=220, y=180)
ventana.mainloop
    
asked by Martin Bouhier 09.10.2017 в 06:58
source

1 answer

1

You must use the command of your tkinter.Button to pass a function that is executed when it is clicked.

Before a few observations

  • The imports of the form from modulo imports* are a bad practice and should be avoided except in very specific cases. Among the reasons this hinders the readability of the code, populates the current namespace without reason and promotes the collision between imports or between the identifiers of the module. Remember two maxims of Python zen, "Explicit better than implied" and "Readability counts".

  • When you apply the place method (or pack , grid , etc) on an instance of a Widget, None is returned. When you do:

    textUsuario = Entry(...).place(...)
    

    Your variable textUsuario does not refer to a Entry but contains None . If you need to use this variable at some point to access the widget in the future you must apply the method in another line:

    textUsuario = Entry(...)
    textUsuario.place(...)
    

    Otherwise, it does not make sense to use the variable, simply create the widget with:

    Entry(...).place(...)
    
  • Lastly, you are not calling the mainloop method. If the code works for you, it is because you execute it in the IDLE possibly (it is created in Tkinter and has its own mainloop ). It should be:

    ventana.mainloop()
    #               ^^
    

Well as I said at the beginning, you only need to create a function that is called when you press the button:

import tkinter as tk


def consulta():
    d = (Inventory_data.get ("Inventory_data")). get (entrada.get())
    # Resto del código

ventana = tk.Tk()
ventana.geometry("500x300+100+100")
ventana.title("MijaGato")

tk.Label(ventana, text="Name Of Tag").place(x=200, y=100)
entrada = tk.StringVar()
tk.Entry(ventana, textvariable=entrada, width=30).place(x=150, y=140)
tk.Button(ventana, text="Start",  command = consulta).place(x=220, y=180)
ventana.mainloop()

The get method is used to obtain the value of stringVar . Obviously, the dictionary Inventory_data must be a global variable so that it can be accessed from the function consulta .

  

Note: In case of using Python 2.x just change the import by import Tkinter as tk .

  

Warning: The function called by the button must return in a short time. It is a blocking call, if it takes to return the mainloop can not update the GUI or respond to events so the application will freeze and stop responding. If you intend to launch an operation that takes time to execute, you must implement some type of concurrency (threads, processes, coroutines, etc) so that the main thread does not block at any time.

    
answered by 09.10.2017 в 10:03