为什么我的kivy程序没有从另一个类调用该函数? [英] why is my kivy program not calling the function from another class?

查看:74
本文介绍了为什么我的kivy程序没有从另一个类调用该函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这更多是一个python问题,然后是一个kivy问题.

I think this is more of a python question, then a kivy question.

class Keyboard正在共享来自类GUI的方法.我创建了一个名为"self.a"的GUI实例,以连接Keyboard类中的2个类.另外,我在MainApp类中创建了一个键盘类实例"self.key.

class Keyboard is sharing a method from class GUI. I created an GUI instance called "self.a" to connected the 2 classes in class Keyboard. Also, I created a keyboard class instance, "self.key in class MainApp.

当我使用此方法时,打印(按下返回按钮"))返回"按钮能够执行打印语句.我知道它为什么有效.当我在方法中使用"self.a.up()"时,返回按钮不会从类GUI调用up()方法.这可能是一个小细节或我所缺少的概念.程序的其余部分没有错误.请帮助并提前致谢.

when I use this method, "print ("Return button is pressed")" the "return" button was able to do the print statement. I understand why it works. When I use the "self.a.up()" in the method, the return button does not called the up() method from class GUI. It's probably a small detail or a concept that I am missing. There is not error from the rest of the program. Please help and thanks in advance.

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.lang.builder import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.modules import keybinding
from kivy.core.window import Window


class GUI(BoxLayout):
    main_display = ObjectProperty()
    text_input = ObjectProperty()


    def plus_1(self):
        self.value = int(self.main_display.text)
        self.main_display.text = str(self.value + 1)
        print (self.main_display.text)

    def minus_1(self):
        self.value = int(self.main_display.text)
        self.main_display.text = str(self.value - 1)


    def up(self):
        self.main_display.text = self.text_input.text
        self.text_input.text = ''

class Keyboard(Widget):

    def __init__(self,):
        super().__init__()
        self.a = GUI()

        self.keyboard = Window.request_keyboard(None, self)
        self.keyboard.bind(on_key_down=self.on_keyboard_down)

    def on_keyboard_down(self, keyboard, keycode, text, modifiers):
        if keycode[1] == 'enter':
            self.a.up()
#           print ("Return button is pressed")
        return True

class MainApp(App):
    def build(self):
        key = Keyboard()
        return GUI()



if __name__=="__main__":
    app = MainApp()
    app.run()

推荐答案

我认为它可以正常工作,但在另一个您看不到的对象中.问题是您在屏幕上显示了GUI类的一个对象.对于Keyboard类,您创建了另一个看不见的对象.因此,解决方案是使用GUI类的一个对象并在MainAppKeyboard类中对其进行操作.像这样:

I think it works fine but in another object that you can't see. The problem is that you show one object of GUI class on screen. And for the Keyboard class you have created another object that you can't see. So the solution is to use one object of GUI class and operate with it in both of MainApp and Keyboard classes. Something like:

class MainApp(App):
    def build(self):
        # here we create an object...
        self.gui = GUI()
        # ...send it to Keyboard class
        key = Keyboard(self.gui)
        # and return it
        return self.gui

class Keyboard(Widget):

    def __init__(self, gui):
        super().__init__()
        # and here we receive that object instead of creating new one
        self.a = gui

这篇关于为什么我的kivy程序没有从另一个类调用该函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆