Python Tkinter scrollbar in multiple tabs

2024/10/11 2:25:43

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)

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.

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

def __add_widget_datawindow(self):'''(FordTIPGui) -> NoneTypePopulate the data_window with widgets.'''# add data_window framedata_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 gridbutton_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_framedisc_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_framenb = ttk.Notebook(nb_frame) # create notebooknb.pack(fill=BOTH, expand=True)# create frames for tabstab_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 tabsnb.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 widgetsself.__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)

pic of the problem: http://www.use.com/supersize.pl?set=3059c6e412c1416578a7

Answer

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)

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

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

https://en.xdnf.cn/q/118377.html

Related Q&A

ValueError: setting an array element with a sequence error is showing

I am trying to convert this column in float type from object type but it is giving this error. import pandas as pddf = pd.DataFrame({col1: [[-0.8783137, 0.05478287, -0.08827557, 0.69203985, 0.06209986]…

Are there any datetime.tzinfo implementations in C?

Ive been working on a Python library that uses a C extension module to do ISO 8601 parsing.Part of that work requires the creation of tzinfo objects, which is by far the slowest part of the parse. Call…

How to open telnet as a textfile rather than a binary file

So I was trying to use the read_until method in telnet but then ran into the error: Traceback (most recent call last): File "c:\Users\Desktop\7DTD Bot\test.py", line 44, in <module> tn.…

The algorithm for dividing the range of subnet

There is a interesting algorithm, wrt dividing the range of subnet.I have a subnet,such as 192.168.1.0/24 or 192.168.1.248/22, and so on. And we know that /24 or /22 stands for networks and (32 - 24) o…

Look if a string starts with the ending characters of another string?

I want to see if ending of one string is similar to starting of another stringif i have a string a="12345678" and b="56789" i want to update a as 123456789these two strings are in …

Dict and List Manipulation Python

I have two files one has key and other has both key and value. I have to match the key of file one and pull the corresponding value from file two. When all the key and value are in plain column format …

Python - input of file path

this code works fine when I put the path of the file myself. but when I want to get it from users raw_input() it doesnt work. what can I do?import string import randomprint "enter number between …

libmproxy and mitmproxy documentation

I am new to the mitmproxy world. I need to write a python script that would log all the requests made from a certain app on Genymotion emulator. Now, I learned that mitmproxy can be helpful for my requ…

Printing mutiple HTML tables using tabulate in python

I want to produce two HTML tables using tabulate package, but I am only able to produce one table and send mail.Is it possible to put more than one html table into a message sent with smtplib and email…

Why am I getting presentation error in python list?

My code is following: d = [int(d) for d in input().split()]l = [] c = 1 for i in range(len(d)):if d[i] not in l:l.append(d[i])c += 1for i in range(len(l)):print(l[i], end=" ")Evaluation: Inpu…