tkinter.Listbox滚动条yview [英] tkinter.Listbox scrolbar yview
问题描述
我在编写Python时再次遇到一些问题,希望寻求帮助.我继续构建列表框"窗口小部件,但无法设置滚动条.我可以将滚动条放在正确的位置,但是,上下滚动不起作用,并弹出一个错误,提示"Object()不带参数".有人可以建议如何解决它吗?我附上下面的代码以供参考.
I again encounter some problems in writing Python and would like to seek my help. I continue to build my Listbox widget but cannot setup a scrollbar. I can put the Scrollbar in the right position, however, the up and down just don't work out and pop up an error saying "Object() takes no parameter". Could anyone advise how to fix it? I attached the code below for reference.
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()
推荐答案
首先,您同时不需要import tkinter
和from tkinter import *
first of all you don't need both import tkinter
and from tkinter import *
- 使用
import
意味着您需要tkinter.'function'
才能调用函数 来自tkinter - 使用
from
意味着您可以像在您的函数中一样调用该函数 开头没有tkinter.
的程序 - 使用
*
意味着从tkinter获得所有功能
- Using
import
means you needtkinter.'function'
to call a function from tkinter - Using
from
means you can call the function as if it were in your program without thetkinter.
at the start - Using
*
means taking all functions from tkinter
我还根据Rawig的答案修正了代码
Also I have fixed the code based on Rawig's answer
import tkinter
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=self.yview)
self.configure(yscrollcommand=s.set)
s.grid(row=0, column=2, sticky=tkinter.N+tkinter.S)
if __name__ == '__main__':
test()
这篇关于tkinter.Listbox滚动条yview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!