如何在Tkinter中为每个框架修复滚动条?

huangapple go评论63阅读模式
英文:

How to fix scrollbars in Tkinter for each frame?

问题

I would like to obtain the following layout:

如何在Tkinter中为每个框架修复滚动条?

With my implementation, I am able to achieve the above design. However, scrollbars are not working neither for left frame nor for right frame. How can I fix it?

Here is my implementation:

import tkinter as tk
from tkinter import ttk
import pandas as pd
import random

# Sample DataFrame
df = pd.DataFrame({
    'ID': [1, 2, 3, 4, 5],
    'Data': [10, 15, 7, 12, 9]
})

def create_radial_chart(frame):
    # Function to create a random radial chart
    # Replace this with your own radial chart creation code
    # For demonstration purposes, we'll just create a random number of labels
    num_labels = random.randint(1, 80)
    labels = [f'Label {i}' for i in range(1, 1500)]
    radial_chart = ttk.Label(frame, text='\n'.join(labels))
    radial_chart.pack()

# Create the main window
root = tk.Tk()
root.title('Tkinter Layout Example')

# Create the dropdown menu
dropdown_var = tk.StringVar(root)
dropdown = ttk.Combobox(root, textvariable=dropdown_var)
dropdown['values'] = df['ID'].unique()
dropdown.pack()

# Create the main frames
main_frame = ttk.Frame(root)
main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

# Create the left frame with a scrollbar
left_frame = ttk.Frame(main_frame)
left_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

left_scrollbar = ttk.Scrollbar(left_frame, orient=tk.VERTICAL)
left_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

left_canvas = tk.Canvas(left_frame, yscrollcommand=left_scrollbar.set)
left_canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

left_scrollbar.config(command=left_canvas.yview)

# Create the right frame with a scrollbar
right_frame = ttk.Frame(main_frame)
right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

right_scrollbar = ttk.Scrollbar(right_frame, orient=tk.VERTICAL)
right_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

right_canvas = tk.Canvas(right_frame, yscrollcommand=right_scrollbar.set)
right_canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

right_scrollbar.config(command=right_canvas.yview)

# Create the radial charts in the left frame
for _ in range(5):
    create_radial_chart(left_canvas)

# Create the middle frames
middle_frame = ttk.Frame(main_frame)
middle_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=10)

upper_middle_frame = ttk.Frame(middle_frame)
upper_middle_frame.pack(fill=tk.BOTH, expand=True)

lower_middle_frame = ttk.Frame(middle_frame)
lower_middle_frame.pack(fill=tk.BOTH, expand=True)

# Create the line plot in the upper middle frame
# Replace this with your own line plot creation code
line_plot = ttk.Label(upper_middle_frame, text='Line Plot')
line_plot.pack()

# Create the data table in the lower middle frame
# Replace this with your own data table creation code
data_table = ttk.Label(lower_middle_frame, text='Data Table')
data_table.pack()

# Create the radial charts in the right frame
for _ in range(5):
    create_radial_chart(right_canvas)

root.mainloop()

The issue in my output is that scrollbars do not work. I expect to have scrollable left and right frames.

英文:

I would like to obtain the following layout:

如何在Tkinter中为每个框架修复滚动条?

With my implementation, I am able to achieve the above design. However, scrollbars are not working neither for left frame nor for right frame. How can I fix it?

Here is my implementation:

import tkinter as tk
from tkinter import ttk
import pandas as pd
import random

# Sample DataFrame
df = pd.DataFrame({
    'ID': [1, 2, 3, 4, 5],
    'Data': [10, 15, 7, 12, 9]
})

def create_radial_chart(frame):
    # Function to create a random radial chart
    # Replace this with your own radial chart creation code
    # For demonstration purposes, we'll just create a random number of labels
    num_labels = random.randint(1, 80)
    labels = [f'Label {i}' for i in range(1, 1500)]
    radial_chart = ttk.Label(frame, text='\n'.join(labels))
    radial_chart.pack()

# Create the main window
root = tk.Tk()
root.title('Tkinter Layout Example')

# Create the dropdown menu
dropdown_var = tk.StringVar(root)
dropdown = ttk.Combobox(root, textvariable=dropdown_var)
dropdown['values'] = df['ID'].unique()
dropdown.pack()

# Create the main frames
main_frame = ttk.Frame(root)
main_frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)

# Create the left frame with a scrollbar
left_frame = ttk.Frame(main_frame)
left_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

left_scrollbar = ttk.Scrollbar(left_frame, orient=tk.VERTICAL)
left_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

left_canvas = tk.Canvas(left_frame, yscrollcommand=left_scrollbar.set)
left_canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

left_scrollbar.config(command=left_canvas.yview)

# Create the right frame with a scrollbar
right_frame = ttk.Frame(main_frame)
right_frame.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

right_scrollbar = ttk.Scrollbar(right_frame, orient=tk.VERTICAL)
right_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

right_canvas = tk.Canvas(right_frame, yscrollcommand=right_scrollbar.set)
right_canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

right_scrollbar.config(command=right_canvas.yview)

# Create the radial charts in the left frame
for _ in range(5):
    create_radial_chart(left_canvas)

# Create the middle frames
middle_frame = ttk.Frame(main_frame)
middle_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=10)

upper_middle_frame = ttk.Frame(middle_frame)
upper_middle_frame.pack(fill=tk.BOTH, expand=True)

lower_middle_frame = ttk.Frame(middle_frame)
lower_middle_frame.pack(fill=tk.BOTH, expand=True)

# Create the line plot in the upper middle frame
# Replace this with your own line plot creation code
line_plot = ttk.Label(upper_middle_frame, text='Line Plot')
line_plot.pack()

# Create the data table in the lower middle frame
# Replace this with your own data table creation code
data_table = ttk.Label(lower_middle_frame, text='Data Table')
data_table.pack()

# Create the radial charts in the right frame
for _ in range(5):
    create_radial_chart(right_canvas)

root.mainloop()

The issue in my output is that scrollbars do not work. I expect to have scrollable left and right frames.

答案1

得分: 1

使用.pack().grid().place()将小部件添加到画布中不会激活关联的滚动条。您需要创建一个帧,并使用.create_window()将其添加到画布中,然后您可以将小部件添加到内部帧中,如果帧的大小大于画布的scrollregion,则会激活滚动条。

# 在左侧画布中添加一个帧
left_internal = ttk.Frame(left_canvas)
left_canvas.create_window(0, 0, window=left_internal, anchor="nw")

# 在右侧画布中添加一个帧
right_internal = ttk.Frame(right_canvas)
right_canvas.create_window(0, 0, window=right_internal, anchor="nw")

# 在左侧和右侧帧中创建径向图表
for _ in range(5):
    create_radial_chart(left_internal)
    create_radial_chart(right_internal)

# 当这些内部帧调整大小时,更新画布的scrollregion
left_internal.bind("<Configure>", lambda e: left_canvas.config(scrollregion=left_canvas.bbox("all")))
right_internal.bind("<Configure>", lambda e: right_canvas.config(scrollregion=right_canvas.bbox("all")))
英文:

Adding widgets into canvas using .pack() or .grid() or .place() will not activate the linked scrollbar. You need to create a frame and add it into the canvas using .create_window(), then you can add widgets to the internal frame which will activate the scrollbar if the size of the frame is larger than the scrollregion of the canavs.

# add a frame into left canvas
left_internal = ttk.Frame(left_canvas)
left_canvas.create_window(0, 0, window=left_internal, anchor=&quot;nw&quot;)

# add a frame into right canvas
right_internal = ttk.Frame(right_canvas)
right_canvas.create_window(0, 0, window=right_internal, anchor=&quot;nw&quot;)

# Create the radial charts in the left and right frame
for _ in range(5):
    create_radial_chart(left_internal)
    create_radial_chart(right_internal)

# update scrollregion of the canvases when those internal frames are resized
left_internal.bind(&quot;&lt;Configure&gt;&quot;, lambda e: left_canvas.config(scrollregion=left_canvas.bbox(&quot;all&quot;)))
right_internal.bind(&quot;&lt;Configure&gt;&quot;, lambda e: right_canvas.config(scrollregion=right_canvas.bbox(&quot;all&quot;)))

huangapple
  • 本文由 发表于 2023年5月25日 20:56:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332519.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定