如何在 Python tkinter 中将滚动条添加到重叠的 canvas
How to add a scrollbar to an overlapping canvas in Python tkinter
我正在尝试为一个学校项目制作一个应用程序,其中一个功能是消息服务,用户可以在其中相互发送消息,这是通过加载消息 GUI 的按钮访问的。我已经在程序开始时添加了一个 canvas 作为主 GUI 的背景,但是对于消息界面,我添加了另一个 canvas 重叠并将使用滚动条滚动消息并显示它们。
本质上,我的问题是我想使用坐标将另一个 canvas 放置在主要 canvas 的顶部,并添加一个仅适合其右侧的滚动条更小 canvas.
提前致谢:)
我尝试使用 pack()、grid()、place() 和 canvas.create_window() 向其添加滚动条,但是在所有情况下,滚动条要么不出现,要么不填满整个第二个 canvas 的右侧,或者未放置在正确的位置。我得到的最接近的是 place() 函数,在该函数中我设法使用 "relx"、"rely" 和 "relheight" 定位滚动条和 canvas,但是滚动条没有滚动canvas.
root = Tk() #creates the GUI
root.title("Eclass")
root.iconbitmap(default='icon.ico') #sets icon
root.state('zoomed')
backgroundcolour = root.cget("bg")
screen_width = root.winfo_screenwidth() - 15
screen_height = root.winfo_screenheight()
canvas = Canvas(root, width = screen_width, height = screen_height)
def messaging():
canvas.delete("all")
msg_canvas = Canvas(canvas, width = 1530, height = 730, bg = "red")
#canvas.create_window(1123,600, window = msg_canvas) this is where I tried to add the second canvas using the create_Window function
msg_canvas.place(relx=0.186, rely=0.227)
msg_scrollbar = Scrollbar(canvas, orient="vertical",command=msg_canvas.yview)
msg_scrollbar.place(relx=0.975,rely=0.2295, relheight = 0.717)
msg_canvas.config(scrollregion=msg_canvas.bbox(ALL))
我希望 canvas 放置在通过 relx 给出的当前坐标内,并依赖或在之前的试验中 canvas.create_window() 中的坐标,然后我希望 msg_scrollbar 位于 msg_canvas 的右侧并填充其 Y(滚动条的高度应与 canvas 的高度相同)。实际上,canvas 和滚动条处于正确的坐标中,但是滚动条即使在移动后也不会滚动 msg_canvas。
, In essence, my problem is that I would like to position another canvas on top of the main canvas using co-ordinates and add a scrollbar which only fits the right-hand side of this smaller canvas.
我建议反对 使用place
。 pack
和 grid
都更适合创建响应分辨率、window 大小和字体变化的代码。
恕我直言,最简单的解决方案是使用 pack
将主要 canvas 放在底部,然后在剩余的 space 中将滚动条放在右侧,次要 canvas在左边。注意:顺序很重要,因为每次调用 pack
时,它都会用完给定一侧的所有 space,从而减少未来小部件可用的 space。
canvas.pack(side="bottom", fill="both", expand=True)
msg_scrollbar.pack(side="right", fill="y")
msg_canvas.pack(side="left", fill="both", expand=True)
您可以使用 grid
完成同样的事情,但它需要额外的代码行来配置行和列的权重。使用 grid
顺序并不重要,因为您要明确设置行和列。
msg_canvas.grid(row=0, column=0, sticky="nsew")
msg_scrollbar.grid(row=0, column=1, sticky="ns")
canvas.grid(row=1, column=0, columnspan=2, sticky="nsew")
root.grid_rowconfigure((0,1), weight=1)
root.grid_columnconfigure(0, weight=1)
我正在尝试为一个学校项目制作一个应用程序,其中一个功能是消息服务,用户可以在其中相互发送消息,这是通过加载消息 GUI 的按钮访问的。我已经在程序开始时添加了一个 canvas 作为主 GUI 的背景,但是对于消息界面,我添加了另一个 canvas 重叠并将使用滚动条滚动消息并显示它们。
本质上,我的问题是我想使用坐标将另一个 canvas 放置在主要 canvas 的顶部,并添加一个仅适合其右侧的滚动条更小 canvas.
提前致谢:)
我尝试使用 pack()、grid()、place() 和 canvas.create_window() 向其添加滚动条,但是在所有情况下,滚动条要么不出现,要么不填满整个第二个 canvas 的右侧,或者未放置在正确的位置。我得到的最接近的是 place() 函数,在该函数中我设法使用 "relx"、"rely" 和 "relheight" 定位滚动条和 canvas,但是滚动条没有滚动canvas.
root = Tk() #creates the GUI
root.title("Eclass")
root.iconbitmap(default='icon.ico') #sets icon
root.state('zoomed')
backgroundcolour = root.cget("bg")
screen_width = root.winfo_screenwidth() - 15
screen_height = root.winfo_screenheight()
canvas = Canvas(root, width = screen_width, height = screen_height)
def messaging():
canvas.delete("all")
msg_canvas = Canvas(canvas, width = 1530, height = 730, bg = "red")
#canvas.create_window(1123,600, window = msg_canvas) this is where I tried to add the second canvas using the create_Window function
msg_canvas.place(relx=0.186, rely=0.227)
msg_scrollbar = Scrollbar(canvas, orient="vertical",command=msg_canvas.yview)
msg_scrollbar.place(relx=0.975,rely=0.2295, relheight = 0.717)
msg_canvas.config(scrollregion=msg_canvas.bbox(ALL))
我希望 canvas 放置在通过 relx 给出的当前坐标内,并依赖或在之前的试验中 canvas.create_window() 中的坐标,然后我希望 msg_scrollbar 位于 msg_canvas 的右侧并填充其 Y(滚动条的高度应与 canvas 的高度相同)。实际上,canvas 和滚动条处于正确的坐标中,但是滚动条即使在移动后也不会滚动 msg_canvas。
, In essence, my problem is that I would like to position another canvas on top of the main canvas using co-ordinates and add a scrollbar which only fits the right-hand side of this smaller canvas.
我建议反对 使用place
。 pack
和 grid
都更适合创建响应分辨率、window 大小和字体变化的代码。
恕我直言,最简单的解决方案是使用 pack
将主要 canvas 放在底部,然后在剩余的 space 中将滚动条放在右侧,次要 canvas在左边。注意:顺序很重要,因为每次调用 pack
时,它都会用完给定一侧的所有 space,从而减少未来小部件可用的 space。
canvas.pack(side="bottom", fill="both", expand=True)
msg_scrollbar.pack(side="right", fill="y")
msg_canvas.pack(side="left", fill="both", expand=True)
您可以使用 grid
完成同样的事情,但它需要额外的代码行来配置行和列的权重。使用 grid
顺序并不重要,因为您要明确设置行和列。
msg_canvas.grid(row=0, column=0, sticky="nsew")
msg_scrollbar.grid(row=0, column=1, sticky="ns")
canvas.grid(row=1, column=0, columnspan=2, sticky="nsew")
root.grid_rowconfigure((0,1), weight=1)
root.grid_columnconfigure(0, weight=1)