why is my kivy program not calling the function from another class?

偶尔善良 提交于 2021-01-29 10:23:36

问题


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

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.

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()

回答1:


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


来源:https://stackoverflow.com/questions/61134317/why-is-my-kivy-program-not-calling-the-function-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!