Tkinter画布滚动条停止工作

时间:2018-08-12 13:05:41

标签: python python-3.x tkinter

最初,当框架和画布尺寸较小时,两个滚动条都可以正常工作。当我增加它们的大小以匹配顶层的大小后,x滚动条消失,y滚动条停止工作。

您可能会说我不需要x滚动条,因为我调整了画布的create_text width属性以适合窗口内的文本,这是正确的,但是我正在尝试学习如何在滚动条上使用滚动条。窗口的最大化按钮处于打开状态,并且处于关闭状态。

我加载读取的文件很长,因此我需要y滚动条才能一直滚动到文本结尾。在查看scrollregion上的一些在线笔记时,我遇到了一个交叉建议,建议使用n,e,w,s协调,但是使用它们时会出错。我使用了scrollregion=(0,0,500,500),但对我来说似乎太有限了。

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 个答案:

答案 0 :(得分:0)

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

希望这可以帮助遇到相同或相似问题的其他人。 :)