简体   繁体   English

如何在Tkinter中将2D数组从一个类传递到另一个类?

[英]How to pass the 2D array from one class to another in Tkinter?

I am trying to write my first app using Tkinter. 我正在尝试使用Tkinter编写我的第一个应用程序。 I can't understand at all how it is possible to pass the data on variables in the 2D array (entered by user) from one class to another. 我根本无法理解如何将2D数组中的变量数据(由用户输入)从一个类传递到另一个类。 Tried to change something, but nothing turned out. 试图改变一些东西,但没有结果。 I will be very grateful for any help or advice. 我将非常感谢任何帮助或建议。

from Tkinter import *

date_index = [2017, 2018, 2019, 2020, 2021]
product_name = ['product 1', 'product 2', 'product 3', 'product 4', 'product 5']

class main: 
    def __init__(self, master):
        self.master = master
        self.master.title('revenue calc')

        Button(self.master, text = 'quantity', command=self.q_button).pack()
        Button(self.master, text = 'prices', command=self.p_button).pack()

        self.master.mainloop()

    def q_button(self):
        q_child(self.master)

    def p_button(self):
        p_child(self.master)

class q_child:
    def __init__(self, master):
        self.slave = Toplevel(master)
        self.slave.title('quantity')

        self.corner_frame = Frame(self.slave)
        self.corner_frame.grid(row=0, column=0)

        self.left_frame = Frame(self.slave)
        self.left_frame.grid(row=1, column=0)

        self.head_frame = Frame(self.slave)
        self.head_frame.grid(row=0, column=1)

        self.main_frame = Frame(self.slave)
        self.main_frame.grid(row=1, column=1)

        self.button_frame = Frame(self.slave)
        self.button_frame.grid(row=2, column=1)


        for i in range(len(product_name)):
            self.testlabel = Label(self.left_frame, text = product_name[i])
            self.testlabel.grid(row=i, column=0)

        for j in range(len(date_index)):
            self.testlabel1 = Label(self.head_frame, width = 5, text = date_index[j])
            self.testlabel1.grid(row=0, column=j)

        self.q0 = []

        for j in range(len(date_index)):
            self.q0.append([])
            for i in range(len(product_name)):
                self.q0[j].append(Entry(self.slave, width = 5, text=""))
                self.q0[j][i].grid(row=j, column=i, in_ = self.main_frame)

        self.save_q_button = Button(self.button_frame, text = 'save', command = self.save_q_data)
        self.save_q_button.pack()

    def save_q_data(self):
        self.q = []

        for j in range(len(date_index)):
            self.q.append([])
            for i in range(len(product_name)):  
                self.q[j].append(float(self.q0[j][i].get()))

class p_child:
    def __init__(self, master):
        self.slave = Toplevel(master)
        self.slave.title('prices')

        self.corner_frame = Frame(self.slave)
        self.corner_frame.grid(row=0, column=0)

        self.left_frame = Frame(self.slave)
        self.left_frame.grid(row=1, column=0)

        self.head_frame = Frame(self.slave)
        self.head_frame.grid(row=0, column=1)

        self.main_frame = Frame(self.slave)
        self.main_frame.grid(row=1, column=1)

        self.button_frame = Frame(self.slave)
        self.button_frame.grid(row=2, column=1)


        for i in range(len(product_name)):
            self.testlabel = Label(self.left_frame, text = product_name[i])
            self.testlabel.grid(row=i, column=0)

        for j in range(len(date_index)):
            self.testlabel1 = Label(self.head_frame, width = 5, text = date_index[j])
            self.testlabel1.grid(row=0, column=j)

        self.p0 = []

        for j in range(len(date_index)):
            self.p0.append([])
            for i in range(len(product_name)):
                self.p0[j].append(Entry(self.slave, width = 5, text=""))
                self.p0[j][i].grid(row=j, column=i, in_ = self.main_frame)

        self.save_p_button = Button(self.button_frame, text = 'save', command = self.save_p_data)
        self.save_p_button.pack()

    def save_p_data(self):
        self.rev = []
        self.revall = []
        self.p = []

        for j in range(len(date_index)):
            self.rev.append([])
            self.p.append([])
            self.s = 0
            for i in range(len(product_name)):  
                self.p[j].append(float(self.p0[j][i].get()))
                self.rev[j].append(self.p[j][i]*q[j][i]) # NameError: global name 'q' is not defined
                self.s += self.rev[j][i]
            self.revall.append(self.s)  

root = Tk()
main(root) 

See below a simplified version of your code which shows how to pass data (in this case the text of a single Entry box) from your TopLevel() window back to your main window. 请参阅下面的代码简化版,其中显示了如何将数据(在本例中为单个Entry框的文本)从TopLevel()窗口传递回主窗口。

Basically, in your q_child class, you store the data you want to return in an attribute called, for example, self.data , so that when you return to the main class, you can access it by calling q.data . 基本上,在q_child类中,将要返回的数据存储在名为self.data的属性中,这样当您返回主类时,可以通过调用q.data来访问它。

You can even store this data in the main window's master attribute under a name like q_data , so that it can be accessed in the p_child class, through master.q_data 您甚至可以将此数据存储在主窗口的master属性中,其名称类似于q_data ,以便可以通过master.q_datap_child类中访问它。

import Tkinter as tk

class main: 
    def __init__(self, master):
        self.master = master
        self.master.q_data = "No data entered"
        tk.Button(self.master, text='quantity', command=self.q_button).pack()
        tk.Button(self.master, text='prices', command=self.p_button).pack()
        self.master.mainloop()

    def q_button(self):
        # Create a TopLevel window to get user input
        q = q_child(self.master)
        # Wait for the user to close the TopLevel window
        self.master.wait_window(q.slave)
        # Store the data input by the user in the main window's "master" attribute
        self.master.q_data = q.data

    def p_button(self):
        # Create a TopLevel window to use the user input data
        p = p_child(self.master)
        # Wait for the user to close the TopLevel window
        self.master.wait_window(p.slave)

class q_child:
    def __init__(self, master):
        # Create a TopLevel window, and grab focus
        self.slave = tk.Toplevel(master)
        self.slave.grab_set()

        # Add an Entry box and a button
        self.q_entry = tk.Entry(self.slave, text="")
        self.q_entry.pack()
        tk.Button(self.slave, text='save', command=self.save_q_data).pack()

        # Initialize the data to be returned
        self.data = "No data entered"

    def save_q_data(self):
        # Update the data to be returned with the Entry box content
        self.data = self.q_entry.get()
        # Close the TopLevel window
        self.slave.destroy()

class p_child:
    def __init__(self, master):
        # Create a TopLevel window, and grab focus
        self.slave = tk.Toplevel(master)
        self.slave.grab_set()

        # Retrieve the user-input data from the "master"
        q_data = master.q_data
        # Show the data on a label
        tk.Label(self.slave, text=q_data).pack()
        # Add a button to go back
        tk.Button(self.slave, text='back', command=self.slave.destroy).pack()

root = tk.Tk()
main(root)

Important : self.master.wait_window(q.slave) ensures that the main class waits for the TopLevel window to be closed before continuing to run. 重要说明self.master.wait_window(q.slave)确保主类在继续运行之前等待TopLevel窗口关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将变量值从一个文件中的一个类传递到另一个文件中的另一个类 python tkinter - How to pass variable value from one class in one file to another class in another file python tkinter Tkinter:如何将变量从一个 class(窗口)传递到另一个 class(窗口)-(不是顶层框架)? - Tkinter: how to pass variable from one class (window) to another class (window) - (not toplevel frame)? 我正在尝试将元素从一个二维数组分配给 python 中的另一个二维数组 - I am trying to assign elements from one 2d array to another 2d array in python 如何将二维数组从 Python 传递到 C? - How to pass a 2d array from Python to C? 如何将2D数组作为参数从C#传递给python - How to pass a 2D array as argument from C# to python 如何将二维数组和向量从 C# 传递到 python? - How to pass a 2D array and vector from C# to python? 从二维数组中选择行而不是在另一个二维数组中 - selecting rows from 2D array not in another 2D array 如何从另一个二维数组切片一个二维数组并获得其余部分 - How to slice a 2D array from another 2D array and get the rest 如何从二维数组中选择值并添加到另一个二维数组 python - How can i select values from 2d array and add to another 2d array python 如何将变量从一个 Tkinter window 传递给另一个? - How can I pass variables from one Tkinter window to another?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM