Tech Learner
Tech Learner

Reputation: 241

condition in entry box in tkinter in python

I have written a script in python 3.4 and i want to put a if-else condition in entry box of tkinter. Is this possible?

Note: Currently the code is not working correctly. Please let me know, what i have written in my code is possible or not ?

from tkinter import *

root = Tk()

a = ""
b = "hello"

e = Entry(root)
e.pack()
e.insert(0, if(len(a) == 0) b else a)

root.mainloop()

Upvotes: 1

Views: 317

Answers (1)

adder
adder

Reputation: 3698

You almost got it:

e.insert(0, b if not a else a)

Also, I suggest you don't check for empty strings like that - the most idiomatic way (in your case) would be:

if not a

You can read more about conditional expressions here.

Upvotes: 1

Related Questions