简体   繁体   English

班级互相影响

[英]Classes interacting each other

i wants to make one class interact in another through a function. 我想通过功能使一个类在另一个类中进行交互。 a button that when clicked, add 1. But when I make the interaction, there is an error saying that Resources has not been defined 一个单击该按钮的按钮,添加1。但是当我进行交互时,出现一个错误,提示尚未定义Resources

this is what i'm tring, but nothing seens to happens 这就是我正在练习的,但是什么也没发生

from tkinter import *


class Caracteristicas:

    def __init__(self,master):

        self.caracteristicas = Frame(master)
        self.caracteristicas.grid(row=1,column=0)

        self.forca = Label(self.caracteristicas, text='FORÇA FÍSICA')
        self.forca.grid(row=0,column=0)

        self.show_forca = Label(self.caracteristicas,text='1')
        self.show_forca.grid(row=0,column=1)

        self.b_forca = Button(self.caracteristicas,text='+',command=self.ad_for)
        self.b_forca.grid(row=0,column=2)

        self.Forca = 1

    def ad_for(self):
        global Forca
        self.Forca += 1
        Vida = self.Forca + 10
        self.show_forca['text'] = self.Forca
        Recursos.show_ferimentos['text'] = Vida


class Recursos:

    def __init__(self, master):

        self.recursos = Frame(master)
        self.recursos.grid(row=1,column=1)

        self.ferimentos = Label(self.recursos, text='FERIMENTOS')
        self.show_ferimentos = Label(self.recursos, text='10')

        self.ferimentos.grid(row=0,column=0)
        self.show_ferimentos.grid(row=1,column=0)


ficha = Tk()
a = Caracteristicas(ficha)
b = Recursos(ficha)
ficha.mainloop()

I would like to know how to make the interaction between the Characteristics class and the Resources class 我想知道如何在Characteristics类和Resources类之间进行交互

I managed to solve the previous problem however, another appeared. 我设法解决了先前的问题,但是又出现了另一个问题。 This is my main program, and the solution proposed does not work in this case. 这是我的主程序,因此建议的解决方案在这种情况下不起作用。

from tkinter import *
from Caracteristicas import Caracteristicas
from Recursos import Recursos

ficha = Tk()
a = Caracteristicas(ficha)
b = Recursos(ficha)
ficha.mainloop()

in the case they are different documents to be used in the main 如果它们是主要使用的不同文件

If you have instances of two classes and you need function in one of them to modify data or call methods on the other, you usually will want to pass a reference to the other object into the one that is going to interact with it. 如果您有两个类的实例,并且其中一个需要函数来修改另一个上的数据或调用方法,则通常需要将对另一个对象的引用传递给将与之交互的对象。

In your code, that probably means you should pass your reference to the Recursos instance into the constructor of you Caracteristicas object, where it can be used later. 在您的代码中,这可能意味着您应该将对Recursos实例的引用传递到Caracteristicas对象的构造函数中,以便以后使用。

Here's a very abbreviated version of what that could look like: 这是一个看起来很简短的版本:

class Caracteristicas:
    def __init__(self, master, resource):
        self.resource = resource    # save value for later
        ... # the rest of the constructor can be the same

    def ad_for(self):
        self.Forca += 1
        Vida = self.Forca + 10
        self.show_forca['text'] = self.Forca
        self.resource.show_ferimentos['text'] = Vida   # main change is here!

You also need to change the code where you create your objects, to something like: 您还需要将创建对象的代码更改为:

b = Recursos(ficha)
a = Caracteristicas(ficha, b) # pass the reference to the resource object in

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM