Tkinter GUI中的重叠滚动条

时间:2019-06-18 19:06:21

标签: python user-interface tkinter scrollbar tkinter-canvas

我在基于tkinter的GUI中有一个双滚动条框架,但是水平滚动条的右侧与垂直条重叠,并且右箭头不可见。

我主要使用以下链接https://lucasg.github.io/2015/07/21/How-to-make-a-proper-double-scrollbar-frame-in-Tkinter/

中的代码

从右下角的图中的图像可以看出,两个滚动条是重叠的,水平滚动条的向右箭头不可见。

从提到的链接中复制代码,因为它是一个最小的可重现示例

import tkinter as tk
from tkinter import ttk


class DoubleScrollbarFrame(ttk.Frame):

  def __init__(self, master, **kwargs):
    '''
      Initialisation. The DoubleScrollbarFrame consist of :
        - an horizontal scrollbar
        - a  vertical   scrollbar
        - a canvas in which the user can place sub-elements
    '''

    ttk.Frame.__init__(self,  master, **kwargs)

    # Canvas creation with double scrollbar
    self.hscrollbar = ttk.Scrollbar(self, orient = tk.HORIZONTAL)
    self.vscrollbar = ttk.Scrollbar(self, orient = tk.VERTICAL)
    self.sizegrip = ttk.Sizegrip(self)
    self.canvas = tk.Canvas(self, bd=0, highlightthickness=0, 
                                  yscrollcommand = self.vscrollbar.set,
                                  xscrollcommand = self.hscrollbar.set)
    self.vscrollbar.config(command = self.canvas.yview)
    self.hscrollbar.config(command = self.canvas.xview)

  def pack(self, **kwargs):
    '''
      Pack the scrollbar and canvas correctly in order to recreate the same look as MFC's windows. 
    '''

    self.hscrollbar.pack(side=tk.BOTTOM, fill=tk.X, expand=tk.FALSE)
    self.vscrollbar.pack(side=tk.RIGHT, fill=tk.Y,  expand=tk.FALSE)
        self.sizegrip.pack(in_ = self.hscrollbar, side = tk.BOTTOM, anchor = "se")
    self.canvas.pack(side=tk.LEFT, padx=5, pady=5,
                                             fill=tk.BOTH, expand=tk.TRUE)

    ttk.Frame.pack(self, **kwargs)



  def get_frame(self):
    '''
      Return the "frame" useful to place inner controls.
    '''
    return self.canvas


if __name__ == '__main__':

  # Top-level frame
  root = tk.Tk()
  root.title( "Double scrollbar with tkinter" )
  root.minsize(width = 600, height = 600)
  frame = DoubleScrollbarFrame(root, relief="sunken")

  # Add controls here
  subframe = ttk.Frame( frame.get_frame() ) 
  txt = ttk.Label(subframe, text="Add things here !")

  #Packing everything
  txt.pack(anchor = 'center', fill = tk.Y, expand = tk.Y)
  subframe.pack(padx  = 15, pady   = 15, fill = tk.BOTH, expand = tk.TRUE)
  frame.pack( padx   = 5, pady   = 5, expand = True, fill = tk.BOTH)


  # launch the GUI
  root.mainloop()

我想知道如何修改代码,以便可以看到水平滚动条的右侧。

0 个答案:

没有答案