首页 > 解决方案 > 使滚动条在填充了超出画布尺寸的文本的画布内工作

问题描述

所以,我有一个我正在编写的小程序。我会尽力只包含相关的代码。这个概念是在一个父框架内有三个框架。左侧有一个带有滚动条的列表框,我完全按照预期工作。中间框架有一个画布,其中填充了文本/信息,这些文本/信息是通过单击左侧列表框中的项目来指定的,并且还在右侧显示一张照片。问题是,中间的信息通常远远超过中心框架的大小,所以我需要一个滚动条。我尝试简单地从列表框中修改我的功能滚动条,但无济于事,以及本网站和其他人的其他人的示例,仍然无济于事。任何关于为什么我的画布滚动条不起作用的见解将不胜感激。

我提供的代码不会运行,但包括所有相关代码以使其运行会使代码非常长,我的印象是我们不喜欢在这里筛选超长的代码,而且任何对 tkinter 有一定了解的人都应该在这里看到一个明显的问题,而无需尝试运行代码。如果我错了,请纠正我。我对使用 SO 还是很陌生,我想以正确的方式做事,同时也不会让潜在的 [临时] 导师感到沮丧。

程序的样子——注意中间的死滚动条

from tkinter import *
root = Tk()
root.configure(bg="gray50")
root.geometry("1650x800")

# *** Function related to info_canvas scrollbar ***
def on_configure(event):  #commenting out "event" changes nothing...why?
    # update scrollregion after starting 'mainloop'
    # when all widgets are in canvas
    info_canvas.configure(scrollregion=info_canvas.bbox('all'))  #doesn't seem to do anything...

# *** Create all Frames/Containers/Canvas ***
mainWindowFrame = Frame(root, bg="gray63", width=1200, height=650)
mainWindowLeftFrame = Frame(mainWindowFrame, bg="gray63", width=400, height=650, padx=10)
mainWindowCenterFrame = Frame(mainWindowFrame, bg="gray63", width=700, height=650)
mainWindowRightFrame = Frame(mainWindowFrame, bg="gray63", width=400, height=650)
info_canvas = Canvas(mainWindowCenterFrame, bg="gray63", bd=0, highlightthickness=0)

# *** Place Canvas ***
info_canvas.grid(row=0, column=0, sticky=NSEW)
info_canvas.create_window((0,0), window=mainWindowCenterFrame, anchor='nw')  #commenting this out changes nothing

# *** Layout parameters of the main containers ***
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)

mainWindowLeftFrame.grid_rowconfigure(1, weight=1)
mainWindowLeftFrame.grid_columnconfigure(0, weight=1)

mainWindowCenterFrame.grid_rowconfigure(1, weight=1)
mainWindowCenterFrame.grid_columnconfigure(0, weight=1)

mainWindowRightFrame.grid_rowconfigure(1, weight=1)
mainWindowRightFrame.grid_columnconfigure(0, weight=1)


# *** Create & Place Scrollbars ***  <---this one works
rocket_scrollbar = Scrollbar(mainWindowLeftFrame, orient=VERTICAL)
lb.configure(yscrollcommand=rocket_scrollbar.set)
lb.grid(row=2, column=0, sticky=N+E+S+W)
lb.columnconfigure(0, weight=1)
rocket_scrollbar.configure(command=lb.yview)
rocket_scrollbar.grid(row=2, column=1, rowspan=40, sticky=N+S+W)

info_scrollbar = Scrollbar(mainWindowCenterFrame, command=info_canvas.yview, orient=VERTICAL)  # <-- this one doesn't
info_scrollbar.grid(row=0, column=1, rowspan=60, sticky=N+S)  
info_canvas.configure(yscrollcommand = info_scrollbar.set)
info_canvas.columnconfigure(0, weight=1)

# *** Bind List Box ***
lb.bind('<<ListboxSelect>>', onselect)

# *** Bind Canvas ***
info_canvas.bind('<Configure>', on_configure) #commenting this out changes nothing...do I need it?

# *** Create all buttons/Labels ***  The labels below are what populate the canvas with text
rocketName = Label(info_canvas, font="-weight bold", bg="gray63", fg="white", pady=0)
rocketName.config(font=("Arial", 20))

infoLabel = Label(info_canvas, bg="gray63", fg="white")
infoLabel.config(font=("Arial", 11))

# *** Placement & Layout of all Buttons ***
rocketName.grid(row=0, column=0)
infoLabel.grid(row=1, column=0, padx=25)
imgLabel = Label(mainWindowRightFrame, bg="gray63", border=0)

root.mainloop()

标签: pythonpython-3.xtkinterscrollbar

解决方案


推荐阅读