【问题标题】:calling a function inside class在类中调用函数
【发布时间】:2018-08-22 17:18:45
【问题描述】:

我怎样才能像这段代码一样在类中调用函数?我试过了,但我不能......这个想法是:我想更新 de 类中的另一个函数,而不是使用这段代码,但没有成功: app=MyApp() app.build.last1.text=time.strftime( "%H:%M:%S \n")

我是怎么做到的?

(...)

class PL1_detect(Button):

    def update(self, dt):
                global ModoPisca
                global t
                global ModoPisca2
                global last2


                #ModoPisca=0
                def tpisca():
                    conta=0
                    global estado_PTT1
                    ##clock(self, dt)
                    app=MyApp()
                    app.build.last1.text=time.strftime("%H:%M:%S \n")
                   #(here the problem: HOW I CAN CALL FUNCTION OF CLASS 

MYAPP(APP) ????

(...)

class MyApp(App):   


    def build(self):


                layout = FloatLayout(size=(800, 600))
        # Make the background gray:
        with layout.canvas.before:
            Color(.2,.2,.2,1)
            self.rect = Rectangle(size=(800,600), pos=layout.pos)


                wimg = Image(source='logo.png', pos=(0,180))

        last1=Label(text=(" "), pos=(-330, -110), font_size='17sp', bold=0)

        return layout

if __name__ == '__main__':
    MyApp().run()

【问题讨论】:

  • 请格式化您的代码。 Python 对格式很敏感,python 程序员也是如此。

标签: python function class call


【解决方案1】:

您可以为该类创建对象 M=我的应用程序() M.build() 将有助于调用该函数。

在你的场景中试试这个

class A():
    def funct1(self):
        sample="hi"

class B():
    def func2(self):
        A.sample="hello"
        print A.sample

a=A()
a.funct1()
b=B()
b.func2()

输出: 你好

【讨论】:

  • 这个想法是:我想更新de类中的另一个函数,而不是使用这段代码,但没有成功:app=MyApp() app.build.last1.text=time.strftime("% H:%M:%S \n") 我是怎么做到的?
  • 这两个类在同一个文件中吗?
  • 如果它在同一个文件中,请尝试使用类名本身而不是对象来调用它。 Myapp.build.last1.text。还要将该 Myapp 类放在调用类之上。
  • 是的,它在同一个文件中...我尝试按照您所说的调用,但没有成功...我收到此错误:AttributeError: 'function' object has no attribute 'last1'跨度>
  • 上面的代码对我有用,我假设您尝试将 last1 文本值变量设置在一个类中的另一个类中。尝试使用类名一次。
猜你喜欢
  • 2012-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-19
相关资源
最近更新 更多