如何从 Kivy GUI 类中的函数调用列表到 Kivy GUI 类中另一个函数中的函数?这两个类都在同一个 .py 文件中

问题描述 投票:0回答:2

我之前有一些 Python 经验,但我是 Kivy GUI 的初学者,目前正在尝试制作问卷调查应用程序。在第一堂课中,我在其中一个函数中创建了一个列表,并希望在第二堂课中使用该列表。我试图在包含它的函数末尾返回列表,并在第二类中使用 class_name().function_name 然后使用 function_name.list_name 调用它,但它仍然无法正常工作。

我的代码如下:

from kivy.uix.spinner import Spinner
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.core.window import Window

class Questionnaire1App(App):

    def build(self):
        #window layout
        self.layout = GridLayout(cols = 1, spacing = 10, size_hint_y = None, padding = (200,50))
        self.layout.bind(minimum_height = self.layout.setter("height"))

        Window.clearcolor = (1,1,1,1)

        #title
        self.ttl = Label(text = "title",
                          height = 50,
                          font_size = 24,
                          color = "black")
        self.layout.add_widget(self.ttl)

        #question + textinput
        self.teach_ask = Label(text = "question?", size_hint_y = None,
                              height = 30, color = "black")
        self.layout.add_widget(self.teach_ask)

        self.teach_in = TextInput(size_hint_y = None, height = 30)
        self.layout.add_widget(self.teach_in)

        #question + spinner
        self.area_ask = Label(text = "question?", size_hint_y = None,
                              height = 30, color = "black")
        self.layout.add_widget(self.area_ask)

        self.area = Spinner(text = "Select item",
                            values = ("1", "2", "3"),
                            size_hint_y = None,
                            height = 30)
        self.layout.add_widget(self.area)

        #question + spinner
        self.ncea_ask = Label(text = "question?", size_hint_y = None,
                              height = 30, color = "black")
        self.layout.add_widget(self.ncea_ask)

        self.ncea = Spinner(text = "Select item",
                            values = ("1", "2"),
                            size_hint_y = None,
                            height = 30)
        self.layout.add_widget(self.ncea)

        #question + spinner
        self.usas_ask = Label(text = "question", size_hint_y = None,
                              height = 30, color = "black")
        self.layout.add_widget(self.usas_ask)

        self.usas = Spinner(text = "Select item",
                            values = ("1", "2"),
                                      size_hint_y = None,
                                      height = 30)
        self.layout.add_widget(self.usas)

        #next button
        self.click = Button(text = "Next", size_hint_y = None, height = 30, on_release = self.next, on_press = self.get_cla) #trying to somehow run the get_cla function before next_function
        self.layout.add_widget(self.click)
        
        #scrolling feature
        self.root = ScrollView(size_hint = (1, None), size = (Window.width, Window.height))
        self.root.add_widget(self.layout)

        return self.root

    def next(self, obj):
        Questionnaire1App().stop()
        Window.close()
        Questionnaire2App().run()

    def get_cla(self, obj1):
        cla = ["", "", ""] #list of strings, can be up to 21 items

        return cla

class Questionnaire2App(App):

    def build(self):
        #window layout
        self.layout = GridLayout(cols = 1, spacing = 10, size_hint_y = None, padding = (200,50))
        self.layout.bind(minimum_height = self.layout.setter("height"))

        Window.clearcolor = (1,1,1,1)

        #question + spinner
        self.class_ask = Label(text = "question?", size_hint_y = None,
                              height = 30, color = "black")
        self.layout.add_widget(self.class_ask)

        clas = Questionnaire1App().get_cla #trying to somehow call the list cla from get_cla function in Questionnaire1App but wouldn't work
        c = clas.cla #give error below

        self.cla = Spinner(text = "Select item",
                           values = (c[0], c[1], c[2]),
                           size_hint_y = None,
                           height = 30)
        self.layout.add_widget(self.cla)

        #scrolling feature
        self.root = ScrollView(size_hint = (1, None), size = (Window.width, Window.height))
        self.root.add_widget(self.layout)

        return self.root
    
if __name__ == '__main__':
    Questionnaire1App().run()

程序到达

c = clas.cla
时给出的错误:

Exception has occurred: AttributeError
'function' object has no attribute 'cla'
  File "C:\...\try.py", line 169, in build
    c = clas.cla
  File "C:\...\try.py", line 138, in next
    Questionnaire2App().run()
  File "C:\...\try.py", line 183, in <module>
    Questionnaire1App().run()
AttributeError: 'function' object has no attribute 'cla'
python function class user-interface kivy
2个回答
0
投票

也许试试这个: 在 class 1

的函数中
self.cla = [your_list]

然后当你在第二堂课需要它时:

C = Questionnaire1App.cla

0
投票

你正在调用函数 without argument 所以,你需要初始化

obj1 = None
以便在没有参数的情况下调用该函数 用下面的代码替换类
get_cla
中的函数
Questionnaire1App

def get_cla(self, obj1 = None):
    cla = ["", "", ""]
    return cla

像下面的代码一样调用该函数的方式不是调用函数的正确方法:

clas = Questionnaire1App().get_cla
c = clas.cla 

你应该像下面的代码那样调用它

clas = Questionnaire1App()
c = clas.get_cla()

函数的返回值将赋值给

c
.

您可能会与

self.click = Button(text = "Next", size_hint_y = None, height = 30, on_release = self.next, on_press = self.get_cla)
这一行混淆。原因是,
on_press = self.get_cla
自动将一个值作为参数传递给函数
get_cla

尝试规定的更改,这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.