Python:从python中的另一个类调用函数

时间:2018-02-01 13:44:00

标签: python python-2.7 kivy kivy-language

可以告诉我如何从另一个班级打电话给方法吗? 当我点击def insert(self):按钮然后我调用def insert(self): #insert data after call add_expense() method RowsExpense().add_expense() 方法。我正在使用代码

RowsExpense().add_expense()

插入数据后,我拨打print('llllll')。因为self.add_widget(r)在控制台中显示而导致调用。但是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() 无效,因为行未显示在屏幕上。

enter image description here

demo.py

<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

demo.kv

<?php get_header(); ?>
<?php
  $args = array(
    'post_type' => 'artists',
  );
  $query = new WP_Query( $args );

 ?>
  <section class="row artists">
    <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
      <div class="col-sm-12 col-md-6 col-lg-3">
        <a href="<?php if( get_post_field('artist_website') ) { ?>
           <?php echo $artist_website; ?>
         <?php } else { the_permalink(); } ?>">
          <img src="<?php get_post_field('artist_feature_image'); ?>" alt="<?php echo the_title() ; ?>">
          <p><?php echo the_title() ;?></p>
        </a>
      </div>

    <?php endwhile; endif; wp_reset_postdata(); ?>

  </section>



<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:1)

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

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

App.get_running_app().root.whateverYouwanttodohere()