gpt4 book ai didi

python - 使用 place 方法在框架中构建滚动条

转载 作者:行者123 更新时间:2023-12-05 04:10:51 24 4
gpt4 key购买 nike

对于困惑的代码,我感到很抱歉,我在尝试解决这个侧边栏时有点慌张。我正在尝试使用 Tkinter 在框架中构建侧边栏,侧边栏出现并且条目填充到窗口中,尽管滚动条没有构建并且不会向下滚动框架。我正在努力弄清楚如何让它发挥作用,非常感谢您的帮助。

        self.x = 50
self.y = 10

if not self.first_search_run:
self.main_frame_content.destroy()
self.main_frame_content_scrollbar.destroy()
self.main_frame_content_canvas.destroy()
self.main_frame_content_handler.destroy()

self.main_frame_content = Frame(main)
self.main_frame_content.place(relx=256/1280, rely=72/720, relwidth=1024/1280, relheight=648/720)

self.main_frame_content_canvas = Canvas(self.main_frame_content)
self.main_frame_content_handler = Frame(self.main_frame_content_canvas)
self.main_frame_content.configure(background=mal_white)

self.main_frame_content_canvas.bind("<Configure>", scroll_handler)

self.main_frame_content_scrollbar = Scrollbar(self.main_frame_content_canvas, orient="vertical", command=self.main_frame_content_canvas)
self.main_frame_content_canvas.configure(yscrollcommand = self.main_frame_content_scrollbar.set)

self.main_frame_content_scrollbar.pack(side="right",fill="y")
self.main_frame_content_canvas.place(relx=0, rely=0, relwidth=1, relheight=1)
self.main_frame_content_canvas.configure(background=mal_white)

self.main_frame_content_handler.configure(background=mal_white)

self.main_frame_content_handler.place(relx=0, rely=0, relwidth=1, relheight=1, anchor=NW)


if self.first_search_run:
self.object_list = []
self.entry_list = []
self.image_list = []
self.entry_builder = 0
self.entry_builder_support = 0
self.first_search_run = False
else:
for entry in self.entry_list:
entry.destroy()

self.object_list = []
self.entry_list = []
self.image_list = []
self.entry_builder = 0
self.entry_builder_support = 0

for entry in self.search_response:

try:
image_load = urllib.request.urlopen(entry[11].text)
except AttributeError:
image_load = urllib.urlopen(entry[11].text)

image_load = image_load.read()
image_load = BytesIO(image_load)
self.image = PIL.Image.open(image_load)
self.image = self.image.resize((144, 203), PIL.Image.ANTIALIAS)
self.entry_image = ImageTk.PhotoImage(self.image)

self.main_content_entry = Canvas(self.main_frame_content_handler)
self.main_content_entry.place(relx=self.x/1024, rely=self.y/648, relwidth=144/1024, relheight=203/648)
self.main_content_entry_tooltip = Tooltip(self.main_content_entry, entry)

self.object_list.append(entry)
self.entry_list.append(self.main_content_entry)
self.image_list.append(self.entry_image)

self.x += 154
self.entry_builder += 1
self.entry_builder_support += 1

if self.entry_builder_support == 6:
self.y += 213
self.x = 50
self.entry_builder_support = 0

self.entry_builder = 0

for image in self.image_list:

self.entry_list[self.entry_builder].create_image(72, 203/2, image = image)

self.entry_builder += 1

最佳答案

题中的代码比较长,无法按原样执行,所以我只是举一个小例子,说明如何通过将框架嵌入 Canvas 来滚动框架。

为了使滚动条能够正确滚动框架,每次框架的大小发生变化时,您都需要更新 Canvas 的 scrollregion 属性。

import tkinter as tk

def on_configure(event):
canvas.configure(scrollregion=canvas.bbox('all'))

root = tk.Tk()

canvas = tk.Canvas(root)
canvas.place(relx=0, rely=0, relheight=1, relwidth=1)

frame = tk.Frame(canvas)
# resize the canvas scrollregion each time the size of the frame changes
frame.bind('<Configure>', on_configure)
# display frame inside the canvas
canvas.create_window(0, 0, window=frame)

scrolly = tk.Scrollbar(root, command=canvas.yview)
scrolly.place(relx=1, rely=0, relheight=1, anchor='ne')
canvas.configure(yscrollcommand=scrolly.set)

# populate the frame
for i in range(20):
tk.Label(frame, text='Label %i' % i).pack()

root.mainloop()

如果您在将此示例改编为您的代码时遇到问题,请告诉我。

关于python - 使用 place 方法在框架中构建滚动条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43795271/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com