Hi all

I am working on a project with Python tk GUI
and trying to make a scrollbar that scrolls more than one canvas horizontally

tried to approach it as option command = ~~ and ~~
but it seems not working

does anyone have any idea on this issue??

Recommended Answers

All 4 Replies

Stick both canvases in a mainframe, and attach the scrollbar to the mainframe.

Stick both canvases in a mainframe, and attach the scrollbar to the mainframe.

Thanks a lot~ zachabesh

Just to make sure i am getting it right
is the mainframe coresponds to the frame that contains
all the canvases?

and how can i make the scrollbar to work with the frame?
would it work the same as setting the scrollbar to scroll the canvas?

Thanks again!

Heh heh, I might have jumped the gun there a bit. It doesn't look like you can attach a scrollbar to a Frame. I just gave you the first answer off the top of my head. Seems like you should be able to...

You end up with the same thing though if you attach the scrollbar to both canvases, here is an example with listboxes:

from Tkinter import *

root = Tk()

class App:
    def __init__(self,master):
        scrollbar = Scrollbar(master, orient=VERTICAL)
        self.b1 = Listbox(master, yscrollcommand=scrollbar.set)
        self.b2 = Listbox(master, yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.b1.pack(side=LEFT, fill=BOTH, expand=1)
        self.b2.pack(side=LEFT, fill=BOTH, expand=1)
    
    def yview(self, *args):
        apply(self.b1.yview, args)
        apply(self.b2.yview, args)


app = App(root)

for item in range(0,100):
    app.b1.insert(0,item)
    app.b2.insert(0,item)
    
root.mainloop()

Run that and see if that's what you're looking for. Obviously horizontal scrolling would have to be xview, but the idea is that you call the xview or yview function for both widgets right after each other.

Thank you, zachabesh
This seems very helpful

I will try to work on with it!

Thanks again!

Heh heh, I might have jumped the gun there a bit. It doesn't look like you can attach a scrollbar to a Frame. I just gave you the first answer off the top of my head. Seems like you should be able to...

You end up with the same thing though if you attach the scrollbar to both canvases, here is an example with listboxes:

from Tkinter import *

root = Tk()

class App:
    def __init__(self,master):
        scrollbar = Scrollbar(master, orient=VERTICAL)
        self.b1 = Listbox(master, yscrollcommand=scrollbar.set)
        self.b2 = Listbox(master, yscrollcommand=scrollbar.set)
        scrollbar.config(command=self.yview)
        scrollbar.pack(side=RIGHT, fill=Y)
        self.b1.pack(side=LEFT, fill=BOTH, expand=1)
        self.b2.pack(side=LEFT, fill=BOTH, expand=1)
    
    def yview(self, *args):
        apply(self.b1.yview, args)
        apply(self.b2.yview, args)


app = App(root)

for item in range(0,100):
    app.b1.insert(0,item)
    app.b2.insert(0,item)
    
root.mainloop()

Run that and see if that's what you're looking for. Obviously horizontal scrolling would have to be xview, but the idea is that you call the xview or yview function for both widgets right after each other.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.