简体   繁体   English

Tkinter 笔记本 - 窗口宽度的选项卡太多

[英]Tkinter notebook - Too many tabs for window width

I am having a problem with my first tkinter (Python 3) notebook app.我的第一个 tkinter (Python 3) 笔记本应用程序出现问题。

The canvas on which the data is displayed only needs to be 775px wide, by 480px high.显示数据的画布只需 775 像素宽,480 像素高。 This is all very well until the number of tabs makes the window wider than that.这一切都很好,直到选项卡的数量使窗口比这更宽。 All the data is placed on one side and the other is a sea of emptyness.所有的数据都放在一边,另一边是一片空旷的海洋。 I have tried to make the notebook widget scrollable but I cannot get it to work.我试图使笔记本小部件可滚动,但我无法让它工作。

Any advice would be greatly received.任何建议都会很受欢迎。

#!/usr/bin/python

# Try to work with older version of Python
from __future__ import print_function

import sys

if sys.version_info.major < 3:
    import Tkinter as tk
    import Tkinter.ttk as ttk
else:
    import tkinter as tk
    import tkinter.ttk as ttk

#============================================================================
#   MAIN CLASS
class Main(tk.Frame):
    """ Main processing
    """
    def __init__(self, root, *args, **kwargs):
        tk.Frame.__init__(self, root, *args, **kwargs)

        self.root = root
        self.root_f = tk.Frame(self.root)

        self.width = 700
        self.height = 300

        # Create a canvas and scroll bar so the notebook can be scrolled
        self.nb_canvas = tk.Canvas(self.root_f, width=self.width, height=self.height)
        self.nb_scrollbar = tk.Scrollbar(self.root_f, orient='horizontal')

        # Configure the canvas and scrollbar to each other
        self.nb_canvas.config(yscrollcommand=self.nb_scrollbar.set,
                              scrollregion=self.nb_canvas.bbox('all'))
        self.nb_scrollbar.config(command=self.nb_canvas.xview)

        # Create the frame for the canvas window, and place
        self.nb_canvas_window = tk.Frame(self.nb_canvas, width=self.width, height=self.height)
        self.nb_canvas.create_window(0, 0, window=self.nb_canvas_window)

        # Put the whole notebook in the canvas window
        self.nb = ttk.Notebook(self.nb_canvas_window)

        self.root_f.grid()
        self.nb_canvas.grid()
        self.nb_canvas_window.grid()
        self.nb.grid(row=0, column=0)
        self.nb_scrollbar.grid(row=1, column=0, sticky='we')

        self.nb.enable_traversal()

        for count in range(20):
            self.text = 'Lots of text for a wide Tab ' + str(count)
            self.tab = tk.Frame(self.nb)
            self.nb.add(self.tab, text=self.text)
            # Create the canvas and scroll bar for the tab contents
            self.tab_canvas = tk.Canvas(self.tab, width=self.width, height=self.height)
            self.tab_scrollbar = tk.Scrollbar(self.tab, orient='vertical')
            # Convigure the two together
            self.tab_canvas.config(xscrollcommand=self.tab_scrollbar.set,
                                      scrollregion=self.tab_canvas.bbox('all'))
            self.tab_scrollbar.config(command=self.tab_canvas.yview)
                # Create the frame for the canvas window
            self.tab_canvas_window = tk.Frame(self.tab_canvas)
            self.tab_canvas.create_window(0, 0, window=self.tab_canvas_window)

            # Grid the content and scrollbar
            self.tab_canvas.grid(row=1, column=0)
            self.tab_canvas_window.grid()
            self.tab_scrollbar.grid(row=1, column=1, sticky='ns')

            # Put stuff in the tab
            for count in range(20):
                self.text = 'Line ' + str(count)
                self.line = tk.Label(self.tab_canvas_window, text=self.text)
                self.line.grid(row=count, column=0)

        self.root.geometry('{}x{}+{}+{}'.format(self.width, self.height, 100, 100))

        return

#   MAIN (MAIN) =======================================================
def main():
    """ Run the app
    """
    # # Create the screen instance and name it
    root = tk.Tk()
    # # This wll control the running of the app.
    app = Main(root)
    # # Run the mainloop() method of the screen object root.
    root.mainloop()
    root.quit()

#   MAIN (STARTUP) ====================================================
#   This next line runs the app as a standalone app
if __name__ == '__main__':
    # Run the function name main()
    main()

OK, so I think I understand now.好的,所以我想我现在明白了。 The tabs are inside the notebook, and inseperable from the notebook.标签在笔记本内部,与笔记本密不可分。 As such, the notebook will always be as wide as the frames within it.因此,笔记本将始终与其中的框架一样宽。 To get the effect I wanted I would need put a canvas into the notebook, and then add the tabs the the canvas.为了获得我想要的效果,我需要在笔记本中放入一个画布,然后在画布上添加标签。 And that is not allowed.这是不允许的。 So back to the drawing board!所以回到绘图板!

If the tabs are of 'constant' width and you know how many will fit the desired (fixed?)size of the window, you could create a "scrolling tabs" widget by hiding the ones that don't fit your width.如果选项卡具有“恒定”宽度并且您知道有多少适合窗口的所需(固定?)大小,您可以通过隐藏不适合您宽度的选项卡来创建“滚动选项卡”小部件。 Create two buttons, left and right that for example hides the one to the right and shows the next hidden one to the left.创建两个按钮,左侧和右侧,例如隐藏右侧的一个并显示左侧的下一个隐藏的按钮。

If there a way to figure out the width of a tab (fontsize in the label, padding etc?) it could be done more 'dynamic'.如果有办法确定选项卡的宽度(标签中的字体大小,填充等?),它可以做得更“动态”。

I would recommend combining the solutions from here: Is there a way to add close buttons to tabs in tkinter.ttk.Notebook?我建议结合这里的解决方案: Is there a way to add close button to tabs in tkinter.ttk.Notebook? (to be able to close a tab) and here: https://github.com/muhammeteminturgut/ttkScrollableNotebook to use buttons instead of a scroll-bar to handle the width issue. (为了能够关闭选项卡)和这里: https : //github.com/muhammeteminturgut/ttkScrollableNotebook使用按钮而不是滚动条来处理宽度问题。 Two changes to get it to work are to load the "notebookTab" variable as the CustomNotebook and to put the closing icon on the left side by switching the order of the innermost children of style.layout in the first answer.使其工作的两个更改是将“notebookTab”变量加载为 CustomNotebook 并通过切换第一个答案中 style.layout 最里面的子项的顺序将关闭图标放在左侧。 This produces a slidable and closeable custom notebook type.这将生成可滑动且可关闭的自定义笔记本类型。

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

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