Python Tkinter-在画布的边缘放置垂直和水平滚动条

时间:2019-06-17 15:34:33

标签: python python-3.x tkinter scrollbar

我想使用Python tkinter将水平和垂直滚动条放在黄色画布的边缘,但是无论我做什么,它都不会移动,而只会停留在画布的外围。为什么?下面是图像:

enter image description here

下面是代码:

 def render_gui(self):

    self.main_window = tk.Tk()
    self.main_window.geometry("1000x600")
    self.main_window.title("Damaged Text Document Virtual Restoration")
    self.main_window.resizable(True, True) 
    self.main_window.configure(background="#d9d9d9")
    self.main_window.configure(highlightbackground="#d9d9d9")
    self.main_window.configure(highlightcolor="black")


    self.main_canvas = tk.Canvas(self.main_window, bg = "yellow")
    self.main_canvas.pack(expand = True, fill = "both")

    vsb = tk.Scrollbar(self.main_canvas, orient="vertical", command=self.main_canvas.yview)
    hsb = tk.Scrollbar(self.main_canvas, orient="horizontal", command=self.main_canvas.xview)
    vsb.grid(row=1, column=50,columnspan = 20, sticky='ns')
    hsb.grid(row=20, column=1,rowspan = 20,sticky = 'wes')

    self.main_canvas.configure(yscrollcommand=vsb.set,xscrollcommand=hsb.set)
    self.main_window.mainloop()

请帮助。

2 个答案:

答案 0 :(得分:3)

问题是您将滚动条放在画布中,而在画布中没有放置其他任何东西。

当您使用grid将内容放入另一个小部件时,空的行和列的大小为零。因此,即使将垂直滚动条放在第50列中,第0列和第2-49列的宽度均为零,因此第50列显示在左侧。 (第1列是水平滚动条的宽度。)

水平滚动条也是如此-您将其放置在第20行中,但第0行和第2-19行的高度为零,因此第20行显示在顶部附近。

通常,将滚动条放在画布内不是一个好主意,因为您在画布上绘制的任何内容都可能被滚动条隐藏或部分隐藏。如果希望它们出现出现在画布中,最简单的解决方案是将画布和滚动条都放在框架中。然后,您可以在画布上关闭边框,然后为框架打开边框。

示例:

import tkinter as tk

class Example():

 def render_gui(self):

    self.main_window = tk.Tk()
    self.main_window.geometry("1000x600")
    self.main_window.title("Damaged Text Document Virtual Restoration")
    self.main_window.resizable(True, True) 
    self.main_window.configure(background="#d9d9d9")
    self.main_window.configure(highlightbackground="#d9d9d9")
    self.main_window.configure(highlightcolor="black")

    canvas_container = tk.Frame(self.main_window, bd=1, relief='sunken')
    canvas_container.pack(expand = True, fill = "both")

    self.main_canvas = tk.Canvas(canvas_container, bg = "yellow")
    vsb = tk.Scrollbar(canvas_container, orient="vertical", command=self.main_canvas.yview)
    hsb = tk.Scrollbar(canvas_container, orient="horizontal", command=self.main_canvas.xview)
    self.main_canvas.configure(yscrollcommand=vsb.set,xscrollcommand=hsb.set)

    vsb.grid(row=0, column=1, sticky="ns")
    hsb.grid(row=1, column=0, sticky="ew")
    self.main_canvas.grid(row=0, column=0, sticky="nsew")
    canvas_container.grid_rowconfigure(0, weight=1)
    canvas_container.grid_columnconfigure(0, weight=1)

    self.main_window.mainloop()

e = Example()
e.render_gui()

答案 1 :(得分:2)

该代码以画布为滚动条的父级。

将滚动条设置为与画布具有相同的父级,并更改周围的一些放置位置,即可使工作变得可行:

import tkinter as tk

class test:
    def __init__(self):
        self.render_gui()

    def render_gui(self):
        self.main_window = tk.Tk()
        self.main_window.geometry("1000x600")
        self.main_window.title("Damaged Text Document Virtual Restoration")
        self.main_window.resizable(True, True) 
        self.main_window.configure(background="#d9d9d9")
        self.main_window.configure(highlightbackground="#d9d9d9")
        self.main_window.configure(highlightcolor="black")

        self.main_canvas = tk.Canvas(self.main_window, bg = "yellow")
        vsb = tk.Scrollbar(self.main_window, orient="vertical", command=self.main_canvas.yview)
        hsb = tk.Scrollbar(self.main_window, orient="horizontal", command=self.main_canvas.xview)
        hsb.pack(side = "bottom", fill = "x")
        vsb.pack(side = "right", fill = "y")
        self.main_canvas.pack(expand = True, fill = "both")

        self.main_canvas.configure(yscrollcommand=vsb.set,xscrollcommand=hsb.set)
        self.main_window.mainloop()

t = test()

enter image description here