I would like detect the text of a tkinter ttk entry widget
I tried with:
import tkinter as tk from tkinter import ttk tk=tk.Tk() e1 = ttk.Entry(tk) if e1 == "good" print("good") You can detect the text in the entry widget by using the get() function on the entry widget. You also need to make sure that you wait until the user has actually entered the text because if you check the contents of the entry widget immediately after creating it, the user won't have entered any text. Instead you need some sort of trigger. This could be a submit button e.g:
import tkinter as tk from tkinter import ttk tk=tk.Tk() e1 = ttk.Entry(tk) # Function that is called to check the text when the button is clicked def checkText(): if e1.get() == "good": print("good") submitButtton = ttk.Button(tk, text="Submit", command=checkText) # Submit button that runs the check text function # Widget positioning e1.grid(row=0,column=0) submitButtton.grid(row=1,column=0) tk.mainloop() This will create a basic GUI with a text entry and submit button, when the submit button is clicked, the checkText() function will be run and the value in the entry will be detected.
For understanding entry widget, user entry doesn't need to be a specific text('good'). With the following, the user entry will get printed(in the terminal or command prompt). (I've given the corrections in your code as comments inside the code)
import tkinter as tk from tkinter import ttk # A function for printing the user entry def print_op(): print(e1.get()) #don't use the same name as tk, as we maybe using for other widgets root=tk.Tk() root.geometry('200x100') e1 = ttk.Entry(root) e1.pack() #pack the widget # On clicking the button, user entry get printed tk.Button(root, text = 'Submit', command = print_op).pack() root.mainloop()
e1 = ttk.Entry(tk)does not wait for user to input and return the entry widget immediately. So the checking after that line is meaningless. Learn about event-driven programming.