【发布时间】:2022-01-02 07:39:18
【问题描述】:
我正在尝试使用 tkinter 和 python 创建客户关系管理程序,并希望根据数据库中的表生成按钮。
这个想法是数据库中的每个表都有一个按钮,以便在需要时可以查看和编辑表。
我想让每个按钮看起来都一样,并在单击时在我的程序的主框架中生成一个表格条目列表。为此,我想扩展 Button() 类,以便在定义 display_items 函数的同时保持一些属性并发:
class TabButton(Button):
def __init__(self, *args, **kwargs):
super().__init__(Button)
self['bg'] = '#a1a1a1'
self['font'] = ('Agency', 24)
def display_items(self, tab):
pass
#mycursor.execute('SELECT * FROM (%s)', tab)
最后一行(上面)是从我的数据库中的正确表中选择数据的内容 - 我在找出课程的其余部分时已将其注释掉。我知道 *args 和 **kwargs 做什么,但我不确定它们在这个 __init__ 函数中的用途(我对类不是很熟悉,并从另一个 Stack Overflow 帖子中复制了这个类)。
为了生成按钮,我引用了一个dict 实例并将每个键分配给一个按钮:
tabs = {
'Table1': '',
'Table2': '',
'Table3': '',
}
for tab in tabs:
row = 0
tabs[tab] = TabButton(side_frame, command=lambda: TabButton.display_items(tab))
tabs[tab].grid(row=row, column=0)
row += 1
问题是,当我运行程序时出现此错误:
AttributeError: type object 'Button' has no attribute 'tk'
欢迎任何和所有的指导!
如果您发现我的代码中有任何其他错误,请指出来吗?我对编程很陌生,它可以节省我在 Stack Overflow 上发表另一篇文章的时间。 :p
谢谢, J
【问题讨论】:
-
这能回答你的问题吗? super() in tkinter application
-
你好 @Thingamabobs ,我想我已经将 super() 更正为 super()__init__(Button),但我现在收到新错误:AttributeError: type object 'Button' has no attribute 'tk'。我绝对需要更深入地了解这里发生的事情。
-
super().__init__(master)master 等于 side_frame。因此,对于更改的最小限度,您需要执行super().__init__(*args),而是在您的 init 方法中执行位置参数,例如:def __init__(self,master, *args, **kwargs) -
嘿,谢谢,但 super() 不要求传递“自我”吗?我可以使用 super().__init__(side_frame) 运行代码,但结果在 GUI 中有点混乱。我哪里错了?
-
查看我的回答,如果对该主题有任何疑问,请告诉我。
标签: python tkinter inheritance parameters crm