使用继承正确扩展tkinter小部件 [英] Correctly extend a tkinter widget using inheritance

查看:81
本文介绍了使用继承正确扩展tkinter小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python类的新手,我还不知道如何处理它们,我保证我已经做过一些研究来解决这个问题,但仍然想不出办法.所以,就这样:

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:

我正在尝试使用python类定义tkinter窗口小部件,以便可以相当快地实现它们.都可以通过按钮和标签很好地工作,但是无法通过输入获得它.我将向您展示如何对按钮和标签进行编码,以说明我也试图处理条目(也许你们可以在必要时帮助我改善这一点?).

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))

这是我的实际问题

如何将其制作为类,以及将小部件制作为类时如何使用 .get() .set()方法?

How to make into a class and, how to use the .get() and .set() methods when the widget is made into a class?

由于我在python类中不是很有经验(特别是将它们与tkinter结合使用),我什至不知道我要问的内容是否可能!p.d .:对不起,如果我的英语不是很好,那不是我的母语

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

推荐答案

问题:如何从 tkinter.Entry

这称为继承.
全部.继承的方法,例如 .get()的行为相同.

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))

用法:

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

print(testEntry.get())

这篇关于使用继承正确扩展tkinter小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆