Hello,


I need your help with something (again):

I need to control two text widgets with one scrollbar and, thanks to this website, I found some code to do it using listboxes (here: http://www.daniweb.com/forums/post940371.html#post940371). I modified it, and it now works with text widgets and pyton 2.x. The problem is: the code is too advanced, and I am unable to implement it within my program... besides, it only works in python 2.x.


I did some research and found out that the reason why it doesn't work with python 3.x is because of the changes in the apply function. So the questions are reduced to just one:

how can I implement the functionality in this code:

from Tkinter import *

root = Tk()

class App:
    def __init__(self,master):
        scrollbar = Scrollbar(master, orient=VERTICAL)
        self.b1 = Text(master, yscrollcommand=scrollbar.set)
        self.b2 = Text(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(1.0,item)
    app.b2.insert(1.0,item)
    
root.mainloop()

into this one:

from tkinter import *

root = Tk()

scrollbar = Scrollbar(root, orient=VERTICAL)
text1 = Text(root, yscrollcommand=scrollbar.set)
text2 = Text(root)

scrollbar.config(command=text1.yview)

text1.grid(row=0, column=1)
text2.grid(row=0, column=2)
scrollbar.grid(row=0, column=3, sticky=N+S)
    
root.mainloop()

remember that the second code is python3.x code

Recommended Answers

All 5 Replies

See http://diveintopython3.org/porting-code-to-python-3-with-2to3.html#apply how you must recode apply.

The first function working both in Python 3 and Python 2 (with expanded text to see scrollbar in action):

try:
    from Tkinter import *
except ImportError: ## Python 3
    from tkinter import *

root = Tk()

class App:
    def __init__(self,master):
        scrollbar = Scrollbar(master, orient=VERTICAL)
        self.b1 = Text(master, yscrollcommand=scrollbar.set)
        self.b2 = Text(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):
        self.b1.yview(*args)
        self.b2.yview(*args)


app = App(root)

for item in range(0,40):
    for i in range(item):
        it=str(i)+' '
        app.b1.insert(1.0,it)
        app.b2.insert(1.0,it)
    app.b1.insert(1.0,'\n')
    app.b2.insert(1.0,'\n')
    
    
root.mainloop()

Oh, yes, like I said before, I had already found out how to get it to work with python 3.x. Actually, it was that same site where I found the information.

The question now is how to do it in the second block of code I showed you above.

And it is here where I need your help: I understand that the magic behind the scrollbar lies in "def yview". But I just can't figure out how to stick it inside my program since my approach seem very different.

(If I may ask a side question, why do some people make tkinter applications in that way? I see it's very different from my approach, (for instance, I never use clases) why is it about? Object oriented GUI making??? And then, what is the name of what I'm doing? Is it a mistake?)

Oh, nevermind (and sorry for the double post, I was unable to edit my previous post). I found out how to place it inside my code:

I just removed the 'self' keywords. The yview function in tonyjv's code is the magic behind the special scrollbar.


It's now working just as I need it to work. Thank you.

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.