tkinter - 如何调用从不同类创建小部件(数据行)的函数

问题描述 投票:0回答:0

我想使用 FrameWithButton 类中的按钮在 RecordsFrame 类中创建标签小部件。我怎么做?我不能只通过按钮命令运行 RecordsFrame

create_column()
func

import customtkinter as ctk
import tkinter as tk
from tkinter import ttk

# example list of records
all_records = [[0, "English", 50, 100, "Quiz", "May 5, 2023", "Passed!"],
               [1, "Math", 50, 100, "Quiz", "May 5, 2023", "Failed."], 
               [2, "History", 50, 100, "Quiz", "May 5, 2023", "Passed!"]]

# frame containing records
class RecordsFrame(ctk.CTkFrame):
    def __init__(self, parent):
        super().__init__(parent)
        self.rowconfigure(0, weight = 1)
        self.columnconfigure(0, weight = 1)
        self.grid(row = 0, column = 0, sticky = 'nsew', padx = 10, pady = 10)

        self.create_column()

    def create_column(self):
        for i in range(len(all_records)):
            rowid_index = i
            rowid_cell = ctk.CTkLabel(
                self,
                text = str(all_records[i][1]),
            )
            rowid_cell.grid(row = i+1, column = 0)
            rowid_index += 1

#frame containing button
class FrameWithButton(ctk.CTkFrame):
    def __init__(self, parent):
        super().__init__(parent)
        self.rowconfigure(0, weight = 1)
        self.columnconfigure(0, weight = 1)

        self.grid(row = 1, column = 0, sticky = 'nsew')

        button = ctk.CTkButton(
            self,
            command = # I want this command to create the widgets in the RecordsFrame class,
            text = "Make widgets"
        )
        button.grid(row = 0, column = 0, sticky = "nsew", padx = 10, pady = 10)

class MyApp(ctk.CTk):
    def __init__(self):
        super().__init__()
        self.geometry("600x600")
        self.title("Test Window")
        self.rowconfigure(0, weight = 1)
        self.rowconfigure(1, weight = 1)
        self.columnconfigure(0, weight = 1)

        self.r_frame = RecordsFrame(self)
        self.button_frame = FrameWithButton(self)

My_app = MyApp()
My_app.mainloop() 

我不太了解 tkinter 如何从不同的框架类运行函数。谁能帮忙?谢谢!

python tkinter tkinter-entry tkinter-button customtkinter
© www.soinside.com 2019 - 2024. All rights reserved.