我正在从“ Programming Python”一书中学习tkinter。在滚动条专用部分中,我遇到了无法修复的问题。
from tkinter import *
class ScrolledList(Frame):
def __int__(self, options, parent=None):
Frame.__init__(self, parent)
self.pack(expand=YES, fill=BOTH)
self.makeWidgets(options)
def hendleList(self, event):
index=self.listbox.curselection()
label=self.listbox.get(index)
self.runCommand(label)
def makeWidget(self, options):
sbar=Scrollbar(self)
list=Listbox(self, relief=SUNKEN)
sbar.config(command=list.yview)
list.config(yscrollcommand=sbar.set)
sbar.pack(side=RIGHT, fill=Y)
list.pack(side=LEFT, expand=YES, fill=BOTH)
pos=0
for label in options:
list.insert(pos, label)
pos+=1
list.bind('<Double-1>', self.handleList)
self.listbox = list
def runCommand(self, selection):
print('You selected:', selection)
if __name__ == '__main__':
options=[('Luberjack-{}'.format(x)) for x in range(20)]
ScrolledList(options).mainloop()
这是我的代码,并且出现了错误:
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/untitled3/venv/scr_list.py", line 34, in <module>
ScrolledList(options).mainloop()
File "C:\Python37-32\lib\tkinter\__init__.py", line 2741, in __init__
Widget.__init__(self, master, 'frame', cnf, {}, extra)
File "C:\Python37-32\lib\tkinter\__init__.py", line 2289, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python37-32\lib\tkinter\__init__.py", line 2259, in _setup
self.tk = master.tk
AttributeError: 'list' object has no attribute 'tk'
以前也有类似的问题,但是我已经在代码中调用了Frame
__init__
答案 0 :(得分:1)
存在一些拼写错误:def __int__
,makeWidget
和hendleList
。经过更正后,它可以正确运行:
from tkinter import *
class ScrolledList(Frame):
def __init__(self, options, parent=None):
Frame.__init__(self, parent)
self.pack(expand=YES, fill=BOTH)
self.makeWidgets(options)
def handleList(self, event):
index=self.listbox.curselection()
label=self.listbox.get(index)
self.runCommand(label)
def makeWidgets(self, options):
sbar=Scrollbar(self)
list=Listbox(self, relief=SUNKEN)
sbar.config(command=list.yview)
list.config(yscrollcommand=sbar.set)
sbar.pack(side=RIGHT, fill=Y)
list.pack(side=LEFT, expand=YES, fill=BOTH)
pos=0
for label in options:
list.insert(pos, label)
pos+=1
list.bind('<Double-1>', self.handleList)
self.listbox = list
def runCommand(self, selection):
print('You selected:', selection)
if __name__ == '__main__':
options=[('Luberjack-{}'.format(x)) for x in range(20)]
ScrolledList(options).mainloop()