【问题标题】:Error when subclassing tk.Listbox (Attribute error object has no attribute 'tk')子类化 tk.Listbox 时出错(属性错误对象没有属性“tk”)
【发布时间】:2019-10-31 21:15:45
【问题描述】:

尝试创建 Listbox 的子类,以便创建新的 KeyListbox

from tkinter import *

class KeyListbox(Listbox):
     def __init__(self,parent,**kwargs):
        super().__init__(self,parent,**kwargs)

root = Tk()
root.title("Key List Test")
testlist=[ "Python" , "Perl" , "C" , "PHP" , "JSP" , "Ruby"]
lijst = KeyListbox(root,selectmode='multiple')
for i in testlist:
    lijst.insert(END,i)

 lijst.pack(root)
 root.mainloop()

AttributeError: 'KeyListbox' 对象没有属性 'tk'

【问题讨论】:

    标签: python tkinter listbox subclass


    【解决方案1】:

    您对super 使用了错误的语法。

    class KeyListbox(Listbox):
        def __init__(self, parent, **kwargs):
            super().__init__(parent, **kwargs)
    

    或者你可以像下面这样调用父类:

    class KeyListbox(Listbox):
        def __init__(self, parent, **kwargs):
            Listbox.__init__(self, parent, **kwargs)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 2018-12-07
      • 2017-11-12
      • 1970-01-01
      相关资源
      最近更新 更多