Tkinter画布滚动条停止工作

2024-05-19 12:51:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

你可能会说我不需要x scrollbar,因为我调整了canvas create\u 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()

Tags: textconfignewlinewidthfillpopframeside
1条回答
网友
1楼 · 发布于 2024-05-19 12:51:37
    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()

希望这能帮助其他人解决同样或类似的问题。:)

相关问题 更多 >