我再次遇到编写Python的一些问题,并希望寻求我的帮助 . 我继续构建我的Listbox小部件但无法设置滚动条 . 我可以将Scrollbar放在正确的位置,但是,向上和向下只是不工作并弹出一个错误,说“对象()没有参数” . 任何人都可以建议如何解决它?我附上以下代码供参考 .
import tkinter
from tkinter import *
def test():
root = tkinter.Tk()
lst = ['1', '2', ' 3', '4', '5', ' 6', '7', '8', ' 9', '10']
a = MovListbox(root, lst)
a.grid(row=0, column=0, columnspan=2, sticky=tkinter.N)
root.mainloop()
class MovListbox(tkinter.Listbox):
def __init__(self, master=None, inputlist=None):
super(MovListbox, self).__init__(master=master)
# Populate the news category onto the listbox
for item in inputlist:
self.insert(tkinter.END, item)
#set scrollbar
s = tkinter.Scrollbar(master, orient=VERTICAL, command=tkinter.YView)
self.configure(yscrollcommand=s.set)
s.grid(row=0, column=2, sticky=tkinter.N+tkinter.S)
if __name__ == '__main__':
test()
1 回答
首先你不需要
import tkinter
和from tkinter import *
使用
import
表示您需要tkinter.'function'
从tkinter调用函数使用
from
意味着您可以像在程序中一样调用该函数,而不是在开始时使用tkinter.
使用
*
表示从tkinter获取所有功能我还根据Rawig的答案修改了代码