Skip to content

Instantly share code, notes, and snippets.

@ihopeudie
Created May 25, 2025 16:14
Show Gist options
  • Save ihopeudie/e203d797e4c05f7c42a8e2e86d6c6b95 to your computer and use it in GitHub Desktop.
Save ihopeudie/e203d797e4c05f7c42a8e2e86d6c6b95 to your computer and use it in GitHub Desktop.
cards with scrolling
import tkinter as tk
from tkinter import ttk
def draw_text(card_frame):
product_type = 'Гостиная'
product_name = 'Диван диванный'
time = 6.0
sku = '10231233'
min_price = 124.0
main_material = 'Щит мебельный'
ttk.Label(card_frame, text=f"{product_type} | {product_name}", font=('Arial', 14)).grid(column=0, row=0, sticky='ew')
ttk.Label(card_frame, text=f"Время изготовления: {time} ч", font=('Arial', 14)).grid(column=1, row=0, sticky='nw')
ttk.Label(card_frame, text=f"{sku}", font=('Arial', 14)).grid(column=0, row=1, sticky='ew')
ttk.Label(card_frame, text=f"Минимальная цена: {min_price}", font=('Arial', 14)).grid(column=0, row=3, sticky='ew')
ttk.Label(card_frame, text=f"Основной материал: {main_material}", font=('Arial', 14)).grid(column=0, row=4, sticky='ew')
def draw_card_frame(table_frame, i):
fake_border_frame = tk.Frame(table_frame, bg='black')
fake_border_frame.grid(row=i, column=0, padx=15, pady=10)
card_frame = tk.Frame(fake_border_frame, height=200, borderwidth=5)
card_frame.pack(padx=4, pady=4, fill='x', expand=True)
draw_text(card_frame)
def draw_scrollable_table_frame(container):
canvas = tk.Canvas(container, bg="#dddddd", highlightthickness=0)
scrollbar = tk.Scrollbar(container, orient="vertical", command=canvas.yview)
scrollable_frame = tk.Frame(canvas, bg="#dddddd")
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(scrollregion=canvas.bbox("all"))
)
canvas_frame = canvas.create_window((0, 0), window=scrollable_frame, anchor="n", width=container.winfo_width())
def resize_canvas(event):
canvas.itemconfig(canvas_frame, width=event.width)
canvas.bind("<Configure>", resize_canvas)
def _on_mousewheel(event):
canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
canvas.bind_all("<MouseWheel>", _on_mousewheel) # Windows/macOS
canvas.bind_all("<Button-4>", lambda e: canvas.yview_scroll(-1, "units")) # Linux scroll up
canvas.bind_all("<Button-5>", lambda e: canvas.yview_scroll(1, "units")) # Linux scroll down
canvas.configure(yscrollcommand=scrollbar.set)
canvas.grid(row=0, column=0, sticky="nsew")
scrollbar.grid(row=0, column=1, sticky="ns")
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
scrollable_frame.grid_columnconfigure(0, weight=1)
for i in range(20):
draw_card_frame(scrollable_frame, i)
def draw_main_frame(root):
frame = tk.Frame(root, bg='#ffffff')
frame.grid(row=0, column=0, sticky=tk.NSEW)
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
draw_scrollable_table_frame(frame)
def main():
root = tk.Tk()
root.geometry("1000x800")
root.title("Test")
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
draw_main_frame(root)
root.mainloop()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment