使滚动条在画布内部工作,并填充超出画布尺寸的文本

时间:2020-08-04 04:31:32

标签: python python-3.x tkinter scrollbar

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

我提供的代码无法运行,但是包括所有相关代码以使其运行将使代码变得很长,而我的印象是我们不希望在这里过长的代码进行筛选,并且即使对tkinter有一定了解的人也应该在这里看到一个明显的问题,而无需尝试运行代码。如果我错了,请纠正我。我对使用SO还是很陌生,我想以正确的方式做事,同时又不妨碍潜在的[临时]导师。

What the program looks like- take note of the dead scrollbar in the center

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

0 个答案:

没有答案