Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

python - Correctly extend a tkinter widget using inheritance

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Question: How to make into a own class from a tkinter.Entry

It's called inheritance.
All. inherited methods, e.g. .get() behave the same.

class MyEntry(tk.Entry):
    def __init__(self, parent, **kwargs):
        # Defaults
        kwargs['fg'] = 'grey'
        super().__init__(parent, **kwargs)

        self.bind('<FocusIn>', self.on_event)
        self.bind('<FocusOut>', self.on_event)
        self.bind('<Return>', self.on_event)

    def on_event(self, event):
        print('on_event type:{}'.format(event.type))

Usage:

testEntry = MyEntry(module, bg=bgColor_1, width = 30)
testEntry.grid(row=0,column = 1)
testEntry.insert(0,'Presupuesto')

print(testEntry.get())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...