如何将单击哪个按钮的信息传递给另一个 class?

How to pass information which button is clicked into another class?

我已经搜索和阅读了,但不太理解其他帖子 and/or 它没有反映我遇到的问题。

我尝试了与类似帖子不同的东西,比如这个:Passing variables, creating instances, self, The mechanics and usage of classes: need explanation,但显然,我不明白 classes,我自己就够了(可以看出我' m 绝对初学者)

from tkinter import Tk, ttk, Frame, Label, StringVar, OptionMenu, Entry, Button, Toplevel
import pymysql as mdb
from functools import partial
from PIL import Image, ImageTk

class Home(object):
    def __init__(self, master):
        self.master = master
        self.master.title("Control Panel")
        self.master.configure(background="#%02x%02x%02x" % (61, 72, 73))
        self.master.geometry("475x190")
        self.master.resizable(width=False, height=False)
        self.main_frame = Frame(master, background="#%02x%02x%02x" % (61, 72, 73))
        self.main_frame.grid(row=1, column=1, columnspan=4, padx=15, pady=15)
        load1 = Image.open('plan.jpg')
        render1 = ImageTk.PhotoImage(load1)
        self.button_plan = Button(self.main_frame, image=render1, command=self.b_clicked('Planning'))
        self.button_plan.image = render1
        self.button_plan.grid(row=1, column=1, padx=12)
        self.label_plan = Label(self.main_frame, text='Planning', font='Calibri 9',
                            background="#%02x%02x%02x" % (61, 72, 73),
                            foreground="#%02x%02x%02x" % (255, 201, 14))
        self.label_plan.grid(row=2, column=1, padx=12)
        load2 = Image.open('consumption.jpg')
        render2 = ImageTk.PhotoImage(load2)
        self.button_consumption = Button(self.main_frame, image=render2, command=self.b_clicked('Consumption'))
        self.button_consumption.image = render2
        self.button_consumption.grid(row=1, column=2, padx=12)
        self.label_consumption = Label(self.main_frame, text='Consumption', font='Calibri 9',
                                   background="#%02x%02x%02x" % (61, 72, 73),
                                   foreground="#%02x%02x%02x" % (255, 201, 14))
        self.label_consumption.grid(row=2, column=2, padx=12)


    def b_clicked(self, id):
        self.button_id = id
        self.master.withdraw()
        self.second_w = Toplevel(self.master)
        self.app = Second(self.second_w, self.button_id)


class Second(object):
    def __init__(self, master, arg1):
        self.master = master
        self.master.title("Second")
        self.master.configure(background="#%02x%02x%02x" % (61, 72, 73))
        self.master.geometry("190x165")
        self.master.resizable(width=False, height=False)
        print(arg1)

root = Tk()
GUI = Home(root)
root.mainloop()

我想将一个变量值传递给另一个 class。在这种情况下,我想从 class 主页获取 self.button_id 值 'Planning' 以传递给 class 第二个。重点是...我不需要解决方案。我真的很想了解它,这将是一个很好的机会,可以通过我自己的例子来学习它!

几件事:

我减少了您的代码以删除所有没有问题的代码。

创建按钮时,command 参数 运行 是回调函数,而不仅仅是分配它。

command=self.b_clicked('Planning')   # Will run self.b_clicked

相反,您可以在此作业中使用 lambda 表达式:

command=lambda:self.b_clicked('Planning')

简而言之:函数名称是 self.b_clicked,通常您会分配 command=self.b_clicked,但如果您想传递参数,则必须使用 lambdapartial,因为以括号结尾的函数名称将只是 运行 函数。

当您创建两个按钮时,您的程序会立即创建两个新的顶层 windows。当您为第二个 window 重用名称 self.second_w 时,您也会丢失对第一个顶层 window 的引用。

此外,您使用名称 id 作为变量并不好,因为 id 是内置函数。

否则它似乎工作得很好:

from tkinter import *

class Home():
    def __init__(self, master):
        self.master = master
        self.main_frame = Frame(master, bg='tan')
        self.main_frame.pack()

        self.button_plan = Button(self.main_frame, text='Plan', 
                                  command=lambda:self.b_clicked('Planning'))
        self.button_plan.grid(row=1, column=1, padx=12)

    def b_clicked(self, idty):      # "id" is a builtin function
        self.button_id = idty
        self.master.withdraw()
        self.second_w = Toplevel(self.master)
        self.app = Second(self.second_w, self.button_id)

class Second():
    def __init__(self, master, arg1):
        self.master = master
        self.master.configure(bg='tomato')
        print(arg1)

root = Tk()
GUI = Home(root)
root.mainloop()