Py学习  »  Python

如何创建Python类(包括类方法)来定义ttk条目小部件?

PythonHacks999 • 3 年前 • 1225 次点击  

我对python类有点陌生,我还不知道如何很好地处理它们,我保证我已经做了一些研究来解决这个问题,但仍然不知道如何处理它们。下面是:

我正在尝试使用python类来定义tkinter小部件,这样我就可以相当快地实现它们。按钮和标签都能很好地使用,但我无法通过输入来实现。我将向你们展示我如何对按钮和标签进行编码,以说明我试图对条目做些什么(如果有必要,也许你们可以帮助我改进这一点?)。

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)

现在,这是我为条目编写的代码,下面是中的答案 this (我添加了一些lambda函数以使其“可重用”)

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() 当小部件被做成一个类时的方法?

由于我对python类不是很有经验(尤其是将它们与tkinter结合起来),我甚至不知道我的要求是否可行! p、 d:抱歉,如果我的英语不是很好,那不是我的母语

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/131995
 
1225 次点击  
文章 [ 1 ]  |  最新文章 3 年前
stovfl
Reply   •   1 楼
stovfl    5 年前

问题 :如何打造自己的品牌 class 从一个 tkinter.Entry

这叫做继承。
全部的继承的方法,例如。 .get() 表现也一样。

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