【问题标题】:Tkinter scrollbar widget not working properly. Scrolling with arrow keys and mouse wheel works, but not by clicking on the scrollbar itselfTkinter 滚动条小部件无法正常工作。使用箭头键和鼠标滚轮滚动有效,但不能通过单击滚动条本身
【发布时间】:2017-09-23 06:12:30
【问题描述】:

我创建了一个类,它是一个带有滚动条的列表框。

我遇到的问题是,如果我单击滚动条箭头或抓住滚动条向上或向下移动它不起作用。

使用鼠标滚轮或键箭头滚动确实有效。

我不确定我的代码有什么问题。我把它附在下面,你可以自己运行看看。

from tkinter import ttk
import tkinter

class App_Menu(tkinter.Tk):

def __init__(self, *args, **kwargs):


    tkinter.Tk.__init__(self, *args, **kwargs)

    tkinter.Tk.wm_title(self,'test')

    folder_button = ttk.Button(self, text = 'test', command=lambda: Listbox_Window([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14], 'title', 'test'))

    folder_button.grid(row=0, column=2, padx=20, pady=5)


class Listbox_Window():


def load_list(self, list1):

    for item in list1:
        self.listbox.insert(tkinter.END, item)
    self.listbox.selection_set(0)

def __init__(self, list1, w_title, instruction):

    self.list_window = tkinter.Tk()
    self.list_window.wm_title(w_title)

    self.list_window.grab_set()

    tkinter.Label(self.list_window, text= instruction, font='Verdana 10 bold').pack(pady=5)
    list_frame = tkinter.Frame(self.list_window)
    list_frame.pack(expand = 'yes', fill='both')

    scrollbar = tkinter.Scrollbar(list_frame)

    self.listbox = tkinter.Listbox(list_frame, width=60, activestyle='dotbox', yscrollcommand=scrollbar.set)

    self.listbox.pack(side = 'left', fill='both', expand = 'yes', padx=(10,0), pady=10)
    scrollbar.pack(side='right', fill = 'y',pady = 10, padx=(0,10))


    self.load_list(list1)



app = App_Menu()
app.mainloop()

【问题讨论】:

    标签: python python-3.x tkinter scrollbar


    【解决方案1】:

    您没有正确配置滚动条。您还没有告诉它单击时要调用什么函数。使用滚动条需要双向配置。必须告知滚动条它滚动哪个小部件,并且需要告知小部件哪个滚动条保持最新。

    在您的情况下,您需要在创建列表框后添加类似的内容:

    scrollbar.configure(command=self.listbox.yview)
    

    【讨论】:

      猜你喜欢
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      • 2018-04-20
      • 2011-02-19
      • 2012-10-19
      • 2013-06-25
      相关资源
      最近更新 更多