python 如何将一个框架放置在另一个框架下?

6qftjkof  于 2023-05-27  发布在  Python
关注(0)|答案(1)|浏览(131)

我有一个布局,其中包括左框架与多个径向图表和滚动条,上中框,显示线图,下中框显示表图像,右框显示另一种径向图表。
如何将表格图表图像放置在折线图下?此框架的大小应与线图框架的大小相同。
下面是我预期布局的图像:

此外,以下是我的实现:

import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk

# Create the main Tkinter window
root = tk.Tk()
root.title("Radial Charts")
root.geometry("1200x600")

# Create the main frame
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)

# Create a canvas for the radial charts
canvas = tk.Canvas(left_frame)
canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

# Create a vertical scrollbar for the radial charts canvas
scrollbar = ttk.Scrollbar(left_frame, orient=tk.VERTICAL, command=canvas.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

# Configure the canvas to use the scrollbar
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind('<Configure>', lambda e: canvas.configure(scrollregion=canvas.bbox('all')))

# Create a frame inside the canvas to hold the radial charts
frame = tk.Frame(canvas)
canvas.create_window((0, 0), window=frame, anchor=tk.NW)

# Create the upper middle frame for the line plot
upper_middle_frame = ttk.Frame(main_frame, width=500, height=600)
upper_middle_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

# Create the lower middle frame
lower_middle_frame = ttk.Frame(main_frame)
lower_middle_frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

# Create a canvas for the line plot
line_plot_canvas = tk.Canvas(upper_middle_frame, width=500, height=600, background='white')
line_plot_canvas.pack(side=tk.TOP, fill=tk.BOTH)

# Load and display the radial charts
checkbuttons = []  # List to store the checkbuttons
selected_checkbuttons = []  # List to store the selected checkbuttons
line_plot_image = None  # Global variable to store line plot image
line_plot_image_tk = None  # Global variable to store line plot image in Tkinter format

def generate_line_plot_path(start_visit, end_visit):
    line_plot_path = f"path/line_plot_{start_visit}_and_{end_visit}.png"
    return line_plot_path

def display_line_plot():
    line_plot_canvas.delete("all")  # Clear the line plot canvas
    if line_plot_image:
        # Adjust the desired width and height values
        desired_width = 900
        desired_height = 600

        # Resize the line plot image
        resized_image = line_plot_image.resize((desired_width, desired_height), Image.ANTIALIAS)

        # Convert the resized image to Tkinter-compatible format
        global line_plot_image_tk
        line_plot_image_tk = ImageTk.PhotoImage(resized_image)

        # Display the resized line plot image on the canvas
        line_plot_canvas.create_image(0, 0, anchor=tk.NW, image=line_plot_image_tk)

# Function to handle checkbox selection
def checkbox_selected():
    global line_plot_image  # Declare line_plot_image as a global variable
    selected_checkbuttons.clear()
    for cb, var in checkbuttons:
        if var.get() == 1:
            selected_checkbuttons.append(cb)
    if len(selected_checkbuttons) >= 2:
        start_visit = int(selected_checkbuttons[0]["text"].split("_")[1])
        end_visit = int(selected_checkbuttons[1]["text"].split("_")[1])
        line_plot_path = generate_line_plot_path(start_visit, end_visit)

        line_plot_image = Image.open(line_plot_path)
        line_plot_image = line_plot_image.resize((500, 600), Image.ANTIALIAS)  # Adjust the size as needed

        display_line_plot()

# Load and display the radial charts
for i in range(1, 10):
    image_path = f"path/visit_{i}.png"  # Replace with the actual image path

    image = Image.open(image_path)
    image = image.resize((400, 400), Image.ANTIALIAS)  # Adjust the size as needed
    image_tk = ImageTk.PhotoImage(image)

    # Create a label to display the image
    label = tk.Label(frame, image=image_tk)
    label.pack()

    # Keep a reference to the image to prevent it from being garbage collected
    label.image = image_tk

    # Create an IntVar for each Checkbutton
    check_var = tk.IntVar()

    # Create a Checkbutton and place it on the bottom-right part of the image
    checkbutton = tk.Checkbutton(label, text=f"visit_{i}", variable=check_var, command=checkbox_selected)
    checkbutton.place(relx=1, rely=1, anchor=tk.SE)

    checkbuttons.append((checkbutton, check_var))  # Add the checkbutton and its associated variable to the list

# Start the Tkinter event loop
root.mainloop()

我将感谢任何帮助,以显示table_1_2.png下的线图。如果用户选择复选框1和2,则应显示line_plot_1_2和table_1_2。如果选择3和4,则应显示line_plot_3_4和table_3_4。
如何添加lower_middle_frame来显示表格图像?

laik7k3q

laik7k3q1#

不要害怕创建额外的框架来帮助您的布局。在这种情况下,我会创建一个框架来容纳右侧的所有三个项目。然后,你可以把每一个都打包到顶部,以你想要的任何顺序。

相关问题