【问题标题】:Tkinter Python managing scrollbar inside the listbox in a canvasTkinter Python在画布的列表框中管理滚动条
【发布时间】:2013-02-02 15:18:11
【问题描述】:

我得到下面的代码,其中包含画布内的列表框,我试图将滚动条仅放在列表框内,但我得到的是滚动条始终设置在整个表单上。你能帮我把滚动条放在列表框中,而不是放在整个画布上。

def __init__(self):
    self.form = Tk()
    self.form.title("Admin");
    self.form.geometry('608x620+400+50')
    self.form.option_add("*font",("Monotype Corsiva",13))
    self.form.overrideredirect(True)
    self.form.resizable(width=FALSE, height=FALSE)
    self.canvas = Canvas(self.form)
    self.canvas.pack(expand=YES,fill=BOTH)
    self.photo = PhotoImage(file='src/back3.gif')
    self.canvas.create_image(-7,-8,image=self.photo,anchor=NW)

    self.scrollbar = tk.Scrollbar(self.canvas, orient="vertical")
    self.lb = tk.Listbox(self.canvas, width=78, height=15,yscrollcommand=self.scrollbar.set)
    self.lb.place(relx=0.04,rely=0.17)
    self.scrollbar.config(command=self.lb.yview)
    self.scrollbar.pack(side="right", fill="none")

【问题讨论】:

    标签: python listbox tkinter scrollbar


    【解决方案1】:

    我建议你创建一个Frame 来包装ListboxScrollbar。这是我写的一个类来做同样的事情,它不完全符合你的代码——我使用grid()而不是pack()——但你明白了。

    class ScrollableListbox(tk.Listbox):
        def __init__(self, master, *arg, **key):
            self.frame = tk.Frame(master)
            self.yscroll = tk.Scrollbar(self.frame, orient=tk.VERTICAL)
            tk.Listbox.__init__(self, self.frame, yscrollcommand=self.yscroll.set, *arg, **key)
            self.yscroll['command'] = self.yview
    
        def grid(self, *arg, **key):
            self.frame.grid(*arg, **key)
            tk.Listbox.grid(self, row=0, column=0, sticky='nswe')
            self.yscroll.grid(row=0, column=1, sticky='ns')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-06
      • 1970-01-01
      • 2017-07-03
      • 2012-04-03
      • 2011-07-19
      • 2014-06-04
      • 2018-07-31
      • 2015-02-08
      相关资源
      最近更新 更多