I am new in python I wanted to make a program that when you write a specific string in entry it compares it with a string and get an output but it doesnt go well ,where is the mistake i have done?
from tkinter import *
from tkinter.ttk import *
app=Tk()
load = Entry(app, width=10)
loadvar = StringVar
z = loadvar.get()
if z == "winner"
Label(app,text="congrats",).grid(row=1,column=0)
app.mainloop()
CodePudding user response:
from tkinter import *
from tkinter.ttk import *
app = Tk()
# Entry
loadvar = StringVar()
load = Entry(app, width=10, textvariable=loadvar)
load.grid(row=0, column=0)
load.focus()
def compare():
if loadvar.get() == "winner":
# Label
Label(app, text="congrats", ).grid(row=1, column=0)
# Button
Button(text="Compare", command=compare).grid(row=0, column=1)
app.mainloop()
In your code, you have not placed the Entry widget yet. Moreover, you should use Buttons and link them with the function you want so that you can carry it out each time you are using it. Otherwise, you are check if the text inside entry is equal to 'winner' even before typing into it.
loadvar = StringVar
This line must be before creating Entry and then put in "textvariable" attribute of the widget.