0
votes

Intitially when the frame and canvas size were smaller both scrollbars worked fine. After I increased their size to match that of the Toplevel, the x scrollbar disappear and the y one stopped working.

You may say I don't need the x scrollbar, because I adjusted the canvas create_text width attribute to fit the text within the window, and you'd be right, but I am trying to learn how to use scrollbars when the window's maximize button is on and when it's off.

The files I load to read are pretty lengthy, so I need the y scrollbar to scroll all the way to the end of the text. Checking some online notes on the scrollregion, I came a cross one that suggested using n,e,w,s coordintates, but when I use them, I get errores. I used scrollregion=(0,0,500,500) but that seems too finite to me.

from tkinter import *
from tkinter import font

newline=''
fileContent=[]
filePath='file.txt'
lines=open(filePath)
newline=lines.read()

w=Tk()

def openViewer():
    pop = Toplevel(w)
    pop.title('Operation Report')
    pop.geometry("750x500+15+20")
    pop.state('zoomed') # Window's Miximize button
    frame=Frame(pop,width=780,height=560)
    frame.grid(row=0,column=0)
    canvas=Canvas(frame, width=780, height=560, background='black')
    canvas.config(scrollregion=(0,0,500,500))
    canvas.pack(side=LEFT, fill=BOTH)
    verdana_font = font.Font(family = "Verdana",size = 13)
    canvas.create_text((30, 0), anchor='nw', text=newline,
                        font=verdana_font, fill = 'light grey',
                        justify=LEFT, width=750)
    hbar=Scrollbar(frame,orient=HORIZONTAL)
    hbar.pack(side=BOTTOM,fill=X)
    hbar.config(command=canvas.xview)
    vbar=Scrollbar(frame,orient=VERTICAL)
    vbar.pack(side=RIGHT,fill=Y)
    vbar.config(command=canvas.yview)
    canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)


btn=Button(w, text='View Content', command=openViewer)
btn.pack()

w.mainloop()
1
The typical way of doing this is canvas.config(scrollregion=canvas.bbox(ALL)). You cannot correctly set the scrollregion at the time you create your canvas, because you don't know how much space the text will take. - jasonharper
@eyllanesc - Thanks for the piece of code. I also noticed that I needed to place canvas.pack() at the end of the declaration. Although the vertical scrollbar is working, I can't see the last line of text. Is this normal? - Bert_AT
Never mind! I resized the canvas and that did the trick. Thanks - Bert_AT
Bert_AT, perhaps you could add an answer explaining what you did to fix it, for future people with the same problem. - Artemis
@ Artemis Fowl - Hi. I have already answered myself several of my own questions, but it seems no one really cares about it, but since you asked, it would be my pleasure to do it. :) - Bert_AT

1 Answers

0
votes
    from tkinter import *
    from tkinter import font

    newline=''
    fileContent=[]
    filePath='C:/path to/ file.txt'
    lines=open(filePath)
    newline=lines.read()

    w=Tk()

    def openViewer():
        pop = Toplevel(w)
        pop.title('Operation Report')
        pop.geometry("750x500+15+20")
        pop.state('zoomed') # Window's Miximize button
        frame=Frame(pop,width=780,height=560)
        frame.grid(row=0,column=0)
        # I decreased the canvas height to show the x scrollbar and removed 
        # the create_text method width attribute to unwrap the text and 
        # activate the horizontal scrollbar
        canvas=Canvas(frame, width=780,height=530, background='black')
        verdana_font = font.Font(family = "Verdana",size = 13)
        canvas.create_text((0, 0), anchor='nw', text=newline,
                            font=verdana_font, fill = 'light grey',
                            justify=LEFT) # Add this width=750 to wrap text
        hbar=Scrollbar(frame,orient=HORIZONTAL)
        hbar.pack(side=BOTTOM,fill=X)
        hbar.config(command=canvas.xview)
        vbar=Scrollbar(frame,orient=VERTICAL)
        vbar.pack(side=RIGHT,fill=Y)
        vbar.config(command=canvas.yview)
        canvas.config(scrollregion=canvas.bbox(ALL))
        canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
        canvas.pack(side=LEFT, expand=True, fill=BOTH)

    btn=Button(w, text='View Content', command=openViewer)
    btn.pack()

    w.mainloop()

Hopefully, this may help someone else with the same or similar problem. :)