alessandro
alessandro

Reputation: 3984

PanedWindow issues with text widgets

I have a couple of issues working with PanedWindow - which I do not well and I'm a bit confused.

In this example I'm trying to put 2 text widgets side-by-side:

 #!/usr/bin/env python

import os
from Tkinter import *
import ttk

root = Tk()
root.geometry("%dx%d+0+0" % (1400,850))

mainpanedframeframe = PanedWindow(root,orient=HORIZONTAL)
mainpanedframeframe.pack(fill=BOTH, expand=TRUE)
auxframe1 = PanedWindow(mainpanedframeframe)
auxframe2 = PanedWindow(mainpanedframeframe)
mainpanedframeframe.add(auxframe1)
mainpanedframeframe.add(auxframe2)


# --------------- chtext -------------
chtext = Text(auxframe1, width=50)
auxframe1.add(chtext)
chyscrollbar=Scrollbar(auxframe1, orient=VERTICAL, command=chtext.yview)
chtext["yscrollcommand"]=chyscrollbar.set
chyscrollbar.pack(side=RIGHT,fill=Y)

# --------------- configtext -------------
configtext = Text(auxframe2, width=150)
auxframe2.add(configtext)
yscrollbar=Scrollbar(auxframe2, orient=VERTICAL, command=configtext.yview)
configtext["yscrollcommand"]=yscrollbar.set
yscrollbar.pack(side=RIGHT, fill=Y)

for i in range(50):
    configtext.insert(INSERT,"test configtext  \n\ntest 2\ntest 3\ntest 4\n\ntest x\n\n\n\n")
    chtext.insert(INSERT,"test chtext  \n\ntest 2\ntest 3\ntest 4\n\ntest x\n\n\n\n")

#------------------------------
mainloop()

it works, but I dont know how to avoid that, at start, the 1st widget is completely shrunken to the left.

As a second question - even more puzzling: if the 2nd text window is a class that simply inherits from Text and nothing else (I will need to do more complicate things, but as it stands in this example it is simply a call to Text() ), the vertical scrollbar is not shown at all!

#!/usr/bin/env python

import os
from Tkinter import *
import ttk

class MyText(Text):
    def __init__(self, parent, *args, **kwargs):
        Text.__init__(self, *args, **kwargs)
        self.parent = parent


root = Tk()
root.geometry("%dx%d+0+0" % (1400,850))

mainpanedframeframe = PanedWindow(root,orient=HORIZONTAL)
mainpanedframeframe.pack(fill=BOTH, expand=TRUE)
auxframe1 = PanedWindow(mainpanedframeframe)
auxframe2 = PanedWindow(mainpanedframeframe)
mainpanedframeframe.add(auxframe1)
mainpanedframeframe.add(auxframe2)


# --------------- chtext -------------
chtext = Text(auxframe1, width=50)
auxframe1.add(chtext)
chyscrollbar=Scrollbar(auxframe1, orient=VERTICAL, command=chtext.yview)
chtext["yscrollcommand"]=chyscrollbar.set
chyscrollbar.pack(side=RIGHT,fill=Y)

# --------------- configtext -------------
configtext = MyText(auxframe2, width=150)
auxframe2.add(configtext)
yscrollbar=Scrollbar(auxframe2, orient=VERTICAL, command=configtext.yview)
configtext["yscrollcommand"]=yscrollbar.set
yscrollbar.pack(side=RIGHT, fill=Y)

for i in range(50):
    configtext.insert(INSERT,"test configtext  \n\ntest 2\ntest 3\ntest 4\n\ntest x\n\n\n\n")
    chtext.insert(INSERT,"test chtext  \n\ntest 2\ntest 3\ntest 4\n\ntest x\n\n\n\n")

#------------------------------
mainloop()

Upvotes: 1

Views: 853

Answers (1)

patthoyts
patthoyts

Reputation: 33223

For question 1: the Tk panedwidget is a geometry manager for one or more panes. When you add a widget as a pane you can specify some options or you can specify these afterwards using the paneconfigure command. eg:

auxframe1.add(chtext, stretch="always")
auxframe.paneconfigure(chtext, stretch="always")
auxframe.panecget(chtext, "stretch")

using stretch="always" should fix your initial problem.

The next point is mentioned in the documentation that "Each pane contains one widget,...". You are managing the text widget using the panedwindow and then also packing a scrollbar into the same window. If you ask each widget about its geometry manager (using chtext.wm_geometry() and chyscrollbar.wm_geometry()) the text widget is being managed by the panedwindow while the scrollbar is managed by the pack geometry manager. This is not a good scheme. What actually happens is the scrollbar overlays part of your text widget, obscruing the edge of the widget. You can observer this is you enter some text and reduce the size of the pane. Once the text hits the edge of the widget it will by default start wrapping. By packing the scrollbar, this does not wrap as soon as the text collides with the edge of the scrollbar but only when the edge of the text widget hits the text. To resolve this you should put the text and scrollbar into one Frame and add that frame to the pane.

I suspect this second point is the issue with your missing text widget in the second part of the question. Your text widget may well just be obscuring your scrollbar. If you either grid or pack the two info a frame then they will both be managed by the same geometry manager and there will be no confusion over how much space each will be given.

Upvotes: 2

Related Questions