【问题标题】:Python Tkinter - Put Vertical and Horizontal Scrollbar at the edge of the canvasPython Tkinter - 将垂直和水平滚动条放在画布的边缘
【发布时间】:2019-10-31 05:43:58
【问题描述】:

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

下面是代码:

 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()

请帮忙。

【问题讨论】:

    标签: python python-3.x tkinter scrollbar


    【解决方案1】:

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

    当您使用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()
    

    【讨论】:

      【解决方案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()
      

      【讨论】:

      • 'pack' 和 'grid' 和 'place' 都可以同时在同一个应用程序中使用。当您尝试在同一个容器中使用它们时,这只是一个问题。在我看来,问题在于将滚动条添加到哪个容器中。
      • 如果您删除It looks like pack and grid are being mixed, without reporting and error:,我将很高兴改变我的投票。这句话暗示它应该抛出一个错误但没有抛出,这是不正确的。
      • @Mike-SMT 我想我已经对其进行了编辑以澄清我的意思,但在 IDLE 中,我确实觉得有点奇怪,我没有收到关于混合包和网格的回溯错误,就像我过去在类似情况下所做的那样。这可能与Scrollbars 的配置方式有关。
      • 您的新陈述仍然不正确。 Pack 仅在根窗口中使用,而网格在 Canvas 内部使用。这些是 2 个独立的容器,在这种情况下,在这个庄园中使用 pack 和 grid 是完全可以接受的。虽然我个人更喜欢自己使用网格。
      • @Mike-SMT 你是对的,我修复了错误,甚至没有注意到它到底是什么。
      【解决方案3】:
      from tkinter import *
      from tkinter import scrolledtext
      
      
      janela = Tk()
      
      
      scroll_x = Scrollbar(janela, orient="horizontal")
      text = scrolledtext.ScrolledText(janela, wrap=NONE)
      
      text.config(xscrollcommand=scroll_x.set)
      scroll_x.configure(command=text.xview)
      
      text.pack(fill=X)
      scroll_x.pack(fill=X)
      
      janela.mainloop()
      

      【讨论】:

        猜你喜欢
        • 2016-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-10
        相关资源
        最近更新 更多