多个标签中的Python Tkinter滚动条 [英] Python Tkinter scrollbar in multiple tabs

查看:465
本文介绍了多个标签中的Python Tkinter滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学会了如何通过将框架嵌入到画布中,然后向其中添加滚动条来制作可滚动框架:

I learned how to make a scrollable frame by embedding the frame in a canvas and then adding a scrollbar to it like this:

def __add_widget_features(self, feat_tab):
    table_frame = ttk.Frame(feat_tab)
    table_frame.pack(fill=BOTH, expand=True)
    self.__make_grid(table_frame)

    ####subframe####              
    self.canvas = Canvas(table_frame, borderwidth=0, background="#ffffff")
    self.frame = LabelFrame(self.canvas, background="#ffffff", text="Timetable")
    self.vsb = ttk.Scrollbar(table_frame, orient="vertical", command=self.canvas.yview)
    self.canvas.configure(yscrollcommand=self.vsb.set)

    self.vsb.pack(side="right", fill="y")
    self.canvas.pack(side="right", fill="both", expand=True)
    self.canvas.create_window((4,4), window=self.frame, anchor="nw", 
                              tags="self.frame")

    self.frame.bind("<Configure>", self.OnFrameConfigure)

我的程序有一个可创建多个选项卡的gui.可滚动的画布在一个选项卡中效果很好,但是当我尝试将相同的代码添加到另一个选项卡时,滚动条在该选项卡上不起作用.如果我在第一个选项卡上注释掉了代码块,则在另一个选项卡上可以正常工作.我已经尝试将第二个选项卡的所有元素命名为不同的名称(以防万一是问题所在),并且我尝试取出自我".名称的一部分,但无济于事.我是Python的新手,所以我确定我缺少一些简单的东西.我尝试发布问题的图片,但是我的代表还不够高.任何帮助都会很棒.

My program has a gui that creates multiple tabs. The scrollable canvas works great in one tab, but when I try to add the same code to a second a tab, the scrollbar doesn't work on that tab. If I comment out the code block on the first tab, it works fine on the other one. I've already tried naming all of the elements of the second tab something different (in case that was the problem) and I've tried taking out the "self." part of the names but none of that helped. I'm quite new to Python so I'm sure I'm missing something simple. I tried posting a picture of the problem but my rep isn't high enough yet. Any help would be great.

更新:根据Brionius的建议,以下是用于创建笔记本的功能:

UPDATE: per Brionius's suggestion, here's the function for creating the notebook:

def __add_widget_datawindow(self):
    '''(FordTIPGui) -> NoneType
    Populate the data_window with widgets.
    '''

    # add data_window frame
    data_frame = ttk.Frame(self.data_window)
    data_frame.pack(fill=BOTH, expand=True)
    self.__make_grid(data_frame)
    self.add_menubar(self.data_window, "other") # add menubar

    # add subframes and make them into a grid
    button_frame = ttk.Frame(data_frame)
    button_frame.grid(row=9, column=0, rowspan=1, columnspan=10,sticky=W+E+N+S)
    self.__make_grid(button_frame)
    nb_frame = ttk.Frame(data_frame)
    nb_frame.grid(row=0, column=0, rowspan=9, columnspan=10, sticky=W+E+N+S)

    ## add widgets to subframes ##

    # button_frame
    disc_button = ttk.Button(button_frame, text="Disconnect", command=lambda:self.disconnect())
    disc_button.grid(row=10, column=0, columnspan=1, sticky=W+E+N+S)

    # nb_frame
    nb = ttk.Notebook(nb_frame) # create notebook
    nb.pack(fill=BOTH, expand=True)

    # create frames for tabs
    tab_frame1 = ttk.Frame(nb)
    tab_frame2 = ttk.Frame(nb)
    tab_frame3 = ttk.Frame(nb)
    tab_frame4 = ttk.Frame(nb)
    tab_frame5 = ttk.Frame(nb)
    self.__make_grid(tab_frame1)
    self.__make_grid(tab_frame2)
    self.__make_grid(tab_frame3)
    self.__make_grid(tab_frame4)
    self.__make_grid(tab_frame5)

    # add tabs
    nb.add(tab_frame1, text='Confidential1 View')
    nb.add(tab_frame2, text='Confidential2 View')
    nb.add(tab_frame3, text='Confidential3 View')
    nb.add(tab_frame4, text='Confidential4 View')
    nb.add(tab_frame5, text='Confidential5 View')

    # add widgets
    self.__add_widget_group(tab_frame1)
    self.__add_widget_feature(tab_frame2)
    self.__add_widget_signal(tab_frame3)
    self.__add_widget_features(tab_frame4)
    self.__add_widget_timetable(tab_frame5)

问题的图片: http://www.use.com/supersize.pl ?set = 3059c6e412c1416578a7

推荐答案

您的父子关系可能做错了.看这段代码:

You are probably doing something wrong with the parent/child relationships. Look at this code:

def __add_widget_timetable(self, timetable_tab):
    table_frame = ttk.Frame(feat_tab)
    table_frame.pack(fill=BOTH, expand=True)
    self.__make_grid(table_frame)

请注意如何传递框架(timetable_tab),创建另一个选项卡(table_frame)并将创建的框架放入其他框架(feat_tab).

Notice how you're passing in a frame (timetable_tab), creating another tab (table_frame), and putting that created frame in some other frame (feat_tab).

似乎有些错误.我的猜测是table_frame应该是timetable_tab的子代.

Something seems rather wrong. My guess is that table_frame should be a child of timetable_tab.

这篇关于多个标签中的Python Tkinter滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆