【问题标题】:Tkinter notebook - Too many tabs for window widthTkinter 笔记本 - 窗口宽度的选项卡太多
【发布时间】:2016-02-01 19:55:29
【问题描述】:

我的第一个 tkinter (Python 3) 笔记本应用程序出现问题。

显示数据的画布只需要宽 775 像素,高 480 像素。这一切都很好,直到选项卡的数量使窗口比这更宽。所有的数据都放在一边,另一边是一片空旷的海洋。我试图让笔记本小部件可滚动,但我无法让它工作。

任何建议都会受到欢迎。

#!/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()

【问题讨论】:

  • 如果您可以将该代码缩减为MCVE,这个问题会好得多。例如,创建一堆选项卡的简单循环就是复制问题所需的全部内容,对吗?
  • 您还可以添加一张显示正在发生的事情的图片吗?很难想象您所描述的内容。
  • 是的,我认为这是很多代码。我会尝试修剪它,但我不知道 MCVE 是什么。我有一个屏幕截图,但我不知道如何上传它。给我一点时间。
  • 所以我没有足够的信誉来嵌入图像,所以我会处理代码。不过不知道 MCVE 是什么。
  • 所以我改了代码;希望能帮助到你。换句话说,我有一个所需大小的窗口,但笔记本选项卡的数量超过了窗口可以容纳的数量。我无法使滚动工作。

标签: python tkinter


【解决方案1】:

好的,我想我现在明白了。标签位于笔记本内部,与笔记本密不可分。因此,笔记本将始终与其中的框架一样宽。为了获得我想要的效果,我需要在笔记本中放置一个画布,然后在画布上添加选项卡。这是不允许的。所以回到绘图板!

【讨论】:

    【解决方案2】:

    如果选项卡的宽度为“恒定”,并且您知道有多少选项卡适合所需的(固定?)窗口大小,则可以通过隐藏不适合您宽度的选项卡来创建“滚动选项卡”小部件.创建两个按钮,左右按钮,例如隐藏右侧的一个并在左侧显示下一个隐藏的按钮。

    如果有办法计算标签的宽度(标签中的字体大小、填充等?)它可以做得更“动态”。

    【讨论】:

      【解决方案3】:

      我建议结合此处的解决方案:Is there a way to add close buttons to tabs in tkinter.ttk.Notebook?(以便能够关闭选项卡)和此处:https://github.com/muhammeteminturgut/ttkScrollableNotebook 使用按钮而不是滚动条来处理宽度问题。 使其工作的两个更改是将“notebookTab”变量加载为 CustomNotebook,并通过在第一个答案中切换 style.layout 最内层子项的顺序将关闭图标放在左侧。这将产生一个可滑动和可关闭的自定义笔记本类型。

      【讨论】:

        猜你喜欢
        • 2018-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-27
        • 1970-01-01
        • 2018-12-13
        • 1970-01-01
        • 2021-04-27
        相关资源
        最近更新 更多