【问题标题】:Python : call function from another class in pythonPython:从python中的另一个类调用函数
【发布时间】:2018-02-01 13:44:00
【问题描述】:

谁能告诉我如何调用另一个类的方法?
当我点击ok 按钮然后我调用def insert(self): 方法。我正在使用代码

def insert(self):
    #insert data after call add_expense() method
    RowsExpense().add_expense()

插入数据后,我调用 RowsExpense().add_expense().its 调用,因为 print('llllll') 显示在控制台中。但 self.add_widget(r) 无法正常工作,因为行未显示在屏幕上。

demo.py

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
from kivy.uix.popup import Popup

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 400)

class User(Popup):
    total_value = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)

    def add_more(self):
        self.ids.rows.add_more()

    def add_more2(self):
        self.ids.rowsExpense.add_expense()

class ExtraPopup(Popup):
    mode = StringProperty("")

    def __init__(self, obj, **kwargs):
        super(ExtraPopup, self).__init__(**kwargs)

    def add_extra(self):
        self.ids.rowsExtra.add_row()

    def insert(self):
        #insert data after call add_expense() method
        RowsExpense().add_expense()


class RowExtra(BoxLayout):
    col_data = ListProperty(["?", "?", "?", "?", "?", "?", "?", "?"])
    button_text = StringProperty("")
    mode = StringProperty("")

    def __init__(self, **kwargs):
        super(RowExtra, self).__init__(**kwargs)
        self.col_data[0] = ''
        self.col_data[1] = ''
        self.col_data[2] = ''


class RowsExtra(BoxLayout):
    #orientation = "vertical"
    row_count = 0
    button_text = StringProperty("")
    textName = StringProperty("")

    def __init__(self, **kwargs):
        super(RowsExtra, self).__init__(**kwargs)

    def add_row(self):
        for x in range(0, 3):
            self.row_count += 1
            r = RowExtra(button_text=str(self.row_count))
            self.add_widget(r)

class Row(BoxLayout):
    col_data = ListProperty(["?", "?", "?", "?", "?"])
    name = ObjectProperty(None)
    button_text = StringProperty("")
    col_data3 = StringProperty("")
    col_data4 = StringProperty("")

    def __init__(self, **kwargs):
        super(Row, self).__init__(**kwargs)

    def add_seller_expenses(self):
        self.mode = "Add"
        popup = ExtraPopup(self)
        popup.ids.rowsExtra.textName = self.ids.name.text
        popup.ids.rowsExtra.add_row()
        popup.open()


class Rows(BoxLayout):
    row_count = 0

    def __init__(self, **kwargs):
        super(Rows, self).__init__(**kwargs)
        self.add_more()

    def add_more(self):
        self.row_count += 1
        self.add_widget(Row(button_text=str(self.row_count)))


class RowExpense(BoxLayout):
    col_data = ListProperty(["?", "?", "?"])
    button_text = StringProperty("")

    def __init__(self, **kwargs):
        super(RowExpense, self).__init__(**kwargs)
        self.col_data[0] = ' '



class RowsExpense(BoxLayout):
    row_count = 0

    def __init__(self, **kwargs):
        super(RowsExpense, self).__init__(**kwargs)

    def add_expense(self):
        print('llllll')
        for x in range(0, 3):
            self.row_count += 1
            r = RowExpense(button_text=str(self.row_count))
            self.add_widget(r)


class rv(BoxLayout):
    data_items = ListProperty([])
    mode = StringProperty("")

    def __init__(self, **kwargs):
        super(rv, self).__init__(**kwargs)


    def add(self):
        self.mode = "Add"
        popup = User()
        popup.open()


class MainMenu(BoxLayout):
    content_area = ObjectProperty()

    def display(self):
        self.rv = rv()
        self.content_area.add_widget(self.rv)

class demo(App):

    def build(self):
        return MainMenu()


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

demo.kv

<Row>:
    size_hint_y: None
    height: self.minimum_height
    height: 40

    Button:
        text: root.button_text
        size_hint_x: None
        top: 200

    TextInput:
        id : name
        text: root.col_data3
        width: 300
    TextInput:
        id: number_input
        text: root.col_data4
        width: 300
        input_filter: 'int'

    Button:
        text: "Button"
        size_hint_x: None
        top: 200
        on_press: root.add_seller_expenses()


<Rows>:
    size_hint_y: None
    height: self.minimum_height
    orientation: "vertical"

<User>:
    id: user
    BoxLayout:
        orientation: "vertical"
        padding : 20, 5


        BoxLayout:
            orientation: "horizontal"
            #padding : 10, 10
            spacing: 10, 10
            size: 450, 40
            size_hint: None, None

            Label:
                size_hint_x: .2
                text: "Number"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .4
                text: "name"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .4
                text: "Value"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

        ScrollView:
            Rows:
                id: rows


        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .2
            size_hint_y: .2

            Button:
                text: "+Add More"
                on_press: root.add_more()

        BoxLayout:
            orientation: "horizontal"
            size_hint: 1, 1

            BoxLayout:
                orientation: "vertical"
                padding: 10, 5
                size_hint: .5, 1

                BoxLayout:
                    orientation: "horizontal"
                    spacing: 10, 10
                    size: 600, 50
                    size_hint: 1, None

                    Label:
                        text: "SN"
                        text_size: self.size
                        size_hint_x: .3
                        halign: "center"

                ScrollView:
                    RowsExpense:
                        id: rowsExpense

                BoxLayout:
                    orientation: "horizontal"
                    size_hint_x: .2
                    size_hint_y: .2

                    Button:
                        text: "+Add More"
                        on_press: root.add_more2()

<rv>:
    BoxLayout:
        orientation: "vertical"

        Button:
            size_hint: .25, .03
            text: "+Add"
            on_press: root.add()

        GridLayout:
            size_hint: 1, None
            size_hint_y: None
            height: 25
            cols: 3

        BoxLayout:
            orientation: "vertical"

<ExtraPopup>:
    title: " Extra"
    title_size: 20
    title_font: "Verdana"
    size_hint: None, None
    size: 400, 400
    auto_dismiss: False


    BoxLayout:
        orientation: "vertical"
        padding : 10, 5
        spacing: 10, 10

        BoxLayout:
            orientation: "horizontal"
            spacing: 10, 10
            size: 550, 30
            size_hint: 1, None

            Label:
                size_hint_x: .3
                text: "SN"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

            Label:
                size_hint_x: .7
                text: "Name"
                text_size: self.size
                valign: 'bottom'
                halign: 'center'

        ScrollView:
            RowsExtra:
                id: rowsExtra

        BoxLayout:
            orientation: "horizontal"
            size_hint_x: .3
            size_hint: .15, .1

            Button:
                text: "+Add More"
                valign: 'bottom'
                on_press: root.add_extra()

        BoxLayout:
            orientation: "horizontal"
            padding : 10, 5
            spacing: 10, 10
            size_hint: .5, .2
            pos_hint: {'x': .25, 'y':.25}
            Button:
                text: 'Ok'
                id: ok_text
                on_release:
                    root.insert()
                    root.dismiss()

            Button:
                text: 'Cancel'
                #size_hint_x: .5
                on_release: root.dismiss()

<RowExtra>:
    size_hint_y: None
    height: self.minimum_height
    height: 30

    Button:
        text: root.button_text
        size_hint_x: .3
        #top: 200

    TextInput:
        text: root.col_data[1]
        size_hint_x: .7

<RowsExtra>:
    size_hint_y: None
    height: self.minimum_height
    orientation: "vertical"


<RowsExpense>:
    size_hint_y: None
    height: self.minimum_height
    orientation: "vertical"


<RowExpense>:
    size_hint_y: None
    height: self.minimum_height
    height: 30

    TextInput:
        text: root.col_data[0]
        multiline: False



<MenuButton@Button>:
    text_size: self.size
    valign: "middle"
    padding_x: 5
    size : (100, 40)
    size_hint : (None, None)
    background_color: 90 , 90, 90, 90
    background_normal: ''
    color: 0, 0.517, 0.705, 1
    border: (0, 10, 0, 0)

<MainMenu>:
    content_area: content_area

    BoxLayout:
        orientation: 'vertical'
        spacing : 10

        BoxLayout:
            canvas.before:
                Rectangle:
                    pos: self.pos
                    size: self.size

            size_hint_y: 2

            MenuButton:
                text: 'Menu'
                size : (50, 12)
                on_release: root.display()

        BoxLayout:
            id: content_area
            size_hint_y: 30

【问题讨论】:

    标签: python python-2.7 kivy kivy-language


    【解决方案1】:

    您正在调用类方法,而您应该调用实例方法。

    您需要在您的根类中存储对您的 RowsExpense 实例的引用,并通过该引用运行您的方法。然后,您可以将该引用传递给您希望通过该类运行该方法的类,作为它的 init 函数中的参数,或者使用普通旧的:

    App.get_running_app().root.whateverYouwanttodohere()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 1970-01-01
      • 2019-12-17
      • 2020-09-13
      • 2015-08-06
      • 2013-06-16
      • 1970-01-01
      相关资源
      最近更新 更多