使用继承正确扩展 tkinter 小部件 [英] Correctly extend a tkinter widget using inheritance
问题描述
我对 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)类按钮动态:def __init__(self, parent, row, col,rowspan, textVar, font, bg, fg,bd,width=None, function=None,sticky=None):变量 = tk.StringVar()变量.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)类标签静态: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)类标签动态:def __init__(self, parent, row, col, textVar, font, bg, fg, sticky=None):变量 = tk.StringVar()变量.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)返回定义焦点输出(条目):entry.delete(0,'end')entry.config(fg=fColor_3)entry.insert(0,'Presupuesto')返回定义输入(条目):x = entry.get()打印(条目.get())焦点输出(条目)返回testEntry = tk.Entry(module,bg=bgColor_1,width = 30, fg='grey')testEntry.grid(row=0,column = 1)testEntry.insert(0,'Presupuesto')testEntry.bind('',lambda x: focusIn(testEntry))testEntry.bind('',lambda x: focusIn(testEntry))testEntry.bind('<Return>',lambda x: enter(testEntry))
这是我的实际问题
如何制作成类,以及如何在将小部件制作成类时使用 .get()
和 .set()
方法?
由于我在 Python 类(尤其是将它们与 tkinter 结合使用)方面经验不足,我什至不知道我要问的是否可行!p.d.:对不起,如果我的英语不是很好,那不是我的母语
问题:如何将
变成自己的tkinter.Entry
class
这叫做继承.
全部.继承的方法,例如.get()
行为相同.
class MyEntry(tk.Entry):def __init__(self, parent, **kwargs):# 默认值kwargs['fg'] = '灰色'super().__init__(parent, **kwargs)self.bind('', self.on_event)self.bind('', self.on_event)self.bind('<返回>', 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')打印(testEntry.get())
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
Question: How to make into a own
class
from atkinter.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())
这篇关于使用继承正确扩展 tkinter 小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!