Я использую холст с прокручиваемой рамкой, но элементы ниже определенного смещения прокрутки не отображаются. Вот пример приложения, демонстрирующего такое поведение. Он содержит 100 квадратов изменяемого размера в длинном списке и ползунок сверху для установки длины стороны. Выше ок. Длина стороны 318 пикселей, самые нижние поля больше не отображаются. Как это исправить?
import tkinter as tk
from tkinter import Canvas, Frame, Scrollbar, Scale
class ScrollableDemoApp:
def __init__(self, root):
root.title("Scrollable Canvas Demo with Resizable Boxes")
root.geometry("500x600")
# Slider to control the size of the boxes
self.box_size = 100 # Default size for the boxes
self.size_slider = Scale(root, from_=100, to=1000, orient="horizontal", label="Box Size", command=self.resize_boxes)
self.size_slider.set(self.box_size)
self.size_slider.pack(fill="x", padx=10, pady=5)
# Create a Canvas widget with a vertical scrollbar
self.canvas = Canvas(root)
self.scrollbar = Scrollbar(root, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side="left", fill="both", expand=True)
self.scrollbar.pack(side="right", fill="y")
# Create a frame inside the canvas
self.scrollable_frame = Frame(self.canvas)
self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
# Add 100 boxes to the scrollable frame
self.boxes = []
for i in range(100):
box = Frame(self.scrollable_frame, bg="white", highlightbackground="black", highlightthickness=1,
width=self.box_size, height=self.box_size)
box.pack_propagate(False) # Prevent the frame from resizing to fit its contents
box.pack(pady=5)
# Add a label to display the index number in each box
label = tk.Label(box, text=f"Box {i + 1}")
label.pack(expand=True)
# Store the box reference for resizing
self.boxes.append(box)
# Update the scroll region to encompass all the content in the scrollable frame
self.scrollable_frame.bind("", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")))
def resize_boxes(self, size):
# Update box size based on the slider value
self.box_size = int(size)
for box in self.boxes:
box.config(width=self.box_size, height=self.box_size)
# Update the scroll region to ensure proper scrolling after resizing
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
if __name__ == "__main__":
root = tk.Tk()
app = ScrollableDemoApp(root)
root.mainloop()
Подробнее здесь: https://stackoverflow.com/questions/791 ... in-tkinter
Элементы в прокручиваемом кадре ниже определенного смещения не отображаются в TkInter. ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Запускать команды в PyGObject, а результаты отображаются в прокручиваемом окне?
Anonymous » » в форуме Python - 0 Ответы
- 6 Просмотры
-
Последнее сообщение Anonymous
-