Im kinda new to python classes and I dont know how to handle them well yet, and I promise I've done some research to solve this problem but still cant figure out how to. So, here it goes:
I'm trying to use python classes to define tkinter widgets so I can implement them rather quickly. It all worked well with buttons and labels but with I cant get it with entries. I'll show you how I've coded buttons and labels to illustrate what im trying to do with entries as well (and maybe you guys can help me improve this if necessary?).
class buttonStatic:
def __init__(self, parent, row, col,rowspan, text, font, bg, fg, bd,width=None, function=None,sticky=None):
button = tk.Button(parent, text=text, font=(font), bg=bg, fg=fg,bd=bd, width=width, command=function)
button.grid(row=row, column=col,rowspan=rowspan, padx=padx, pady=pady, sticky=sticky)
class buttonDinamic:
def __init__(self, parent, row, col,rowspan, textVar, font, bg, fg,bd,width=None, function=None,sticky=None):
variable = tk.StringVar()
variable.set(textVar)
button = tk.Button(parent, textvariable=variable, font=(font), bg=bg, fg=fg,bd=bd, width=width, command=function)
button.grid(row=row, column=col,rowspan=rowspan, padx=padx, pady=pady, sticky=sticky)
class labelStatic:
def __init__(self, parent, row, col, text, font, bg, fg, sticky=None):
label = tk.Label(parent, text=text, font=(font), bg=bg, fg=fg)
label.grid(row=row, column=col, padx=padx, pady=pady, sticky=sticky)
class labelDinamic:
def __init__(self, parent, row, col, textVar, font, bg, fg, sticky=None):
variable = tk.StringVar()
variable.set(textVar)
label = tk.Label(parent, textvariable=variable, font=(font), bg=bg, fg=fg)
label.grid(row=row, column=col, padx=padx, pady=pady, sticky=sticky)
Now, this is what I've coded for the entries, following the answer in this (I've added some lambda functions to make it "reusable")
def focusIn(entry):
entry.delete(0,'end')
entry.config(fg=fColor_1)
return
def focusOut(entry):
entry.delete(0,'end')
entry.config(fg=fColor_3)
entry.insert(0,'Presupuesto')
return
def enter(entry):
x = entry.get()
print(entry.get())
focusOut(entry)
return
testEntry = tk.Entry(module,bg=bgColor_1,width = 30, fg='grey')
testEntry.grid(row=0,column = 1)
testEntry.insert(0,'Presupuesto')
testEntry.bind('<FocusIn>',lambda x: focusIn(testEntry))
testEntry.bind('<FocusOut>',lambda x: focusIn(testEntry))
testEntry.bind('<Return>',lambda x: enter(testEntry))
Here is my actual question
How to make into a class and, how to use the .get()
and .set()
methods when the widget is made into a class?
As im not very experienced in python classes (and especially combining them with tkinter) I dont even know if what I'm asking is possible!
p.d.: Sorry if my english is not very good, it's not my native language
See Question&Answers more detail:
os