繁体   English   中英

借助 canvas 在 Tkinter 中添加水平滚动条

[英]Adding a horizontal scrollbar in Tkinter with the help of canvas

我正在尝试在 Tkinter 中添加水平和垂直滚动条。 垂直滚动条有效,但水平滚动条无效,我不知道为什么。 实际上,水平滚动条出现在 window 上,但不能滚动。 如果有人能解决这个问题,我会非常高兴。

提前致谢!

import tkinter as tk
from tkinter import ttk

class Example(tk.Frame):
    def __init__(self, parent):

        tk.Frame.__init__(self, parent)
        self.canvas = tk.Canvas(self, borderwidth=0, background="#ffffff")
        self.frame = tk.Frame(self.canvas, background="#ffffff")
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.vsb.set)
        self.hsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
        self.canvas.configure(xscrollcommand=self.hsb.set)

        self.vsb.pack(side="right", fill="y")
        self.hsb.pack(side="bottom", fill="x")
        self.canvas.pack(side="left", fill="both", expand=True)
        self.canvas_window = self.canvas.create_window((4,4), window=self.frame, anchor="nw", tags="self.frame")

        self.frame.bind("<Configure>", self.onFrameConfigure) #bind an event whenever the size of the viewPort frame changes.
        self.canvas.bind("<Configure>", self.onFrameConfigure)#Enables to scroll
        #self.canvas.unbind_all("<MouseWheel>")

        self.items_variables1 = {"f1_Apple1": 1, "f1_Apple2": 1, "f1_Apple3": 1, "f1_Apple4": 0, "f1_Apple5": 0,"f1_Apple6": 0, "f1_Apple7": 1, "f1_Apple8": 0, "f1_Apple9": 0,"f1_Applex": 0, "f1_Appley": 0, "f1_Applez": 0}
        self.items_variables2 = {"f1_Banana1": 1, "f1_Banana2": 0, "f1_Banana3": 1, "f1_Banana4": 0, "f1_Banana5": 0, "f1_Banana6": 0,  "f1_Banana7": 1, "f1_Banana8": 0, "f1_Banana9": 0, "f1_Bananax": 0, "f1_Bananay": 0, "f1_Bananaz": 1}

        self.tabControl = ttk.Notebook(self.frame, width=100, height=1100) # Create a tabcontrol
        self.tab1 = ttk.Frame(self.tabControl) #Create a tab
        self.tab1.pack()#(fill=BOTH, expand=1)
        self.tabControl.add(self.tab1, text="TEST TAB1")
        self.tabControl.pack(expand=1, fill="both") #Pack to make visible

        self.label1 = tk.Label(self.tab1, text='ADD 1', fg='white', bg='black')
        self.getIt1 = ttk.LabelFrame(self.tab1, labelwidget=self.label1)
        self.getIt1.grid(column=0, row=0, padx=20, pady=10)

        self.label2 = tk.Label(self.tab1, text = 'ADD 2', fg='white', bg='black')
        self.getIt2 = ttk.LabelFrame(self.tab1, labelwidget=self.label2)
        self.getIt2.grid(column=0, row=1, padx=20, pady=10)

        varsapple = self.check_button(2, 1, self.getIt1, self.items_variables1)
        print(varsapple["f1_Apple1"].get())
        varsbanana = self.check_button(2, 1, self.getIt2, self.items_variables2)
        print(varsbanana["f1_Banana1"].get())

    def onFrameConfigure(self, event):
        '''Reset the scroll region to encompass the inner frame'''
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def onCanvasConfigure(self, event):
        '''Reset the canvas window to encompass inner frame when required'''
        canvas_width = event.width
        self.canvas.itemconfig(self.canvas_window, width=canvas_width) #whenever the size of the canvas changes alter the window region respectively.

    def check_button(self, r, c, frame, items):
        row = r
        for item in items:
            items[item] = tk.IntVar(value=items[item])
            checkbox = tk.Checkbutton(frame, text=item[3:-1], variable=items[item])
            if items[item].get() == 1:
                checkbox.select()
                checkbox.configure(state='disable')
            else:
                checkbox.deselect()
                print("in")
            checkbox.grid(column=c, row=row, sticky=tk.W)
            row += 1
        return items

if __name__ == "__main__":
    root = tk.Tk()
    example = Example(root)
    example.pack(fill="both", expand=True)
    root.mainloop()

您已将笔记本的宽度明确设置为 100 像素。 这小于 canvas 的宽度,所以没有什么可以滚动的。 如果您希望它更宽以便可以滚动,请使其比 canvas 更宽。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM