<small id='Yh61j'></small><noframes id='Yh61j'>

    1. <i id='Yh61j'><tr id='Yh61j'><dt id='Yh61j'><q id='Yh61j'><span id='Yh61j'><b id='Yh61j'><form id='Yh61j'><ins id='Yh61j'></ins><ul id='Yh61j'></ul><sub id='Yh61j'></sub></form><legend id='Yh61j'></legend><bdo id='Yh61j'><pre id='Yh61j'><center id='Yh61j'></center></pre></bdo></b><th id='Yh61j'></th></span></q></dt></tr></i><div id='Yh61j'><tfoot id='Yh61j'></tfoot><dl id='Yh61j'><fieldset id='Yh61j'></fieldset></dl></div>
        <bdo id='Yh61j'></bdo><ul id='Yh61j'></ul>

      <legend id='Yh61j'><style id='Yh61j'><dir id='Yh61j'><q id='Yh61j'></q></dir></style></legend>
    2. <tfoot id='Yh61j'></tfoot>

        Python/Kivy:从一个类调用函数到另一个类并在 Python 中显示小部件

        时间:2023-06-07

        <tfoot id='YjNII'></tfoot>
        • <bdo id='YjNII'></bdo><ul id='YjNII'></ul>
          <legend id='YjNII'><style id='YjNII'><dir id='YjNII'><q id='YjNII'></q></dir></style></legend>

            <small id='YjNII'></small><noframes id='YjNII'>

                  <tbody id='YjNII'></tbody>
                • <i id='YjNII'><tr id='YjNII'><dt id='YjNII'><q id='YjNII'><span id='YjNII'><b id='YjNII'><form id='YjNII'><ins id='YjNII'></ins><ul id='YjNII'></ul><sub id='YjNII'></sub></form><legend id='YjNII'></legend><bdo id='YjNII'><pre id='YjNII'><center id='YjNII'></center></pre></bdo></b><th id='YjNII'></th></span></q></dt></tr></i><div id='YjNII'><tfoot id='YjNII'></tfoot><dl id='YjNII'><fieldset id='YjNII'></fieldset></dl></div>
                • 本文介绍了Python/Kivy:从一个类调用函数到另一个类并在 Python 中显示小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 Python-2.7 和 Kivy.当我运行 test.py 时,会显示一个显示按钮.当我单击显示按钮时,会显示一个标签和值.我正在从数据库中获取它,但现在我将它定义为一个静态数组.

                  I am using Python-2.7 and Kivy. When I run test.py then a show button shows. When I click on the show button then a label and value shows. I am fetching it from the database but now I defined it as a static array.

                  当我点击 Item1 标签然后调用 def open_form 并在使用 id 调用 EditPopup(self) 后传递 id 11 并显示一个带有值的弹出窗口.

                  When I click on Item1 label then call def open_form and pass id 11 after calling EditPopup(self) with id and show a popup with value.

                  之后我更改 itemCode 并单击确定按钮,然后它会更新数据库中的数据.

                  After that I change itemCode and click on the ok button then it updates data in the database.

                  之后我调用 Invoice().abc() 然后它在 Pycharm 控制台中打印更新的数据,但不显示 layout.add_widget 中的最新数据(弹出窗口).

                  After that I call Invoice().abc() then it prints the updated data in Pycharm console but not showing latest the data in layout.add_widget(Popup window).

                  谁能告诉我哪里出错了?

                  Can someone tell me where am I making a mistake?

                  from kivy.uix.label import Label
                  from kivy.uix.screenmanager import Screen
                  from kivy.app import App
                  from kivy.core.window import Window
                  from functools import partial
                  from kivy.uix.popup import Popup
                  from kivy.properties import BooleanProperty, ListProperty, StringProperty, ObjectProperty, NumericProperty
                  import sqlite3 as lite
                  #con = lite.connect('test.db')
                  
                  #con.text_factory = str
                  #cur = con.cursor()
                  Window.clearcolor = (0.5, 0.5, 0.5, 1)
                  Window.size = (600, 600)
                  
                  class MyLabel(Label):
                      pass
                  
                  
                  class EditPopup(Popup):
                      mode = StringProperty("")
                      label_rec_id = StringProperty("Id")
                      col_data = ListProperty(["?", "?", "?"])
                      index = NumericProperty(0)
                  
                      def __init__(self, obj, **kwargs):
                          super(EditPopup, self).__init__(**kwargs)
                  
                          if obj.mode == "Update":
                              #cur.execute("SELECT * FROM `item` WHERE itemId=?", (edit_id,))
                              #row = cur.fetchone()
                              row = (11, 'Item1', '1001')
                              print(row[0])
                              self.col_data[0] = str(row[0])
                              self.col_data[1] = row[1]
                              self.col_data[2] = row[2]
                  
                      def update(self,obj):
                          #cur.execute("UPDATE `item` SET itemName=?, itemCode=? WHERE itemId=?",
                                  #('Item1', 9999, 11))
                          #con.commit()
                          Invoice().abc()
                  
                  class Invoice(Screen):
                      def __init__(self, **kwargs):
                          super(Invoice, self).__init__(**kwargs)
                  
                      def abc(self):
                          #fetching from database
                          #cur.execute("SELECT * FROM `item` order by itemId asc")
                          #rows = cur.fetchall()
                          rows = [(11, 'Item1', '1001'), (12, 'Item2', '2001'), (13, 'Item3', '102')]
                          print(rows)
                          layout = self.ids['invoices']
                          for row in rows:
                              layout.add_widget(MyLabel(text=str('[ref=world]' + row[1]) + '[/ref]',
                                                        size_hint_x=.35,
                                                        halign='left',
                                                        markup=True,
                                                        on_ref_press=partial(self.open_form, row[0])))
                  
                      def open_form(self, id, *args):
                          global edit_id
                          edit_id = id
                          self.mode = "Update"
                          popup = EditPopup(self)
                          popup.open()
                  
                  class Test(App):
                  
                      def build(self):
                          return Invoice()
                  
                  if __name__ == '__main__':
                      Test().run()
                  

                  test.kv

                  <Button@Button>:
                      font_size: 15
                      font_name: 'Verdana'
                      size_hint_y:None
                      height: 30
                  
                  <MyLabel>:
                      font_size: 15
                      font_name: 'Verdana'
                      size_hint_y:None
                      height: 30
                      text_size: self.size
                      valign: 'middle'
                      canvas.before:
                          Color:
                              rgb: .6, .6, .6
                          Rectangle:
                              pos: self.pos
                              size: self.size
                  
                  <EditPopup>:
                      title: self.mode + " Item"
                      size_hint: None, None
                      title_size: 20
                      title_font: "Verdana"
                      size: 400, 275
                      auto_dismiss: False
                  
                      BoxLayout:
                          orientation: "vertical"
                          GridLayout:
                              cols: 2
                              #backgroun_color: 0, 0.517, 0.705, 1
                              spacing: 10, 10
                              padding: 20, 20
                              Label:
                                  text: root.label_rec_id
                                  text_size: self.size
                              Label:
                                  id: itemId
                                  text: root.col_data[0]
                                  text_size: self.size
                              Label:
                                  text: "Item Name"
                                  text_size: self.size
                                  valign: 'middle'
                              TextInput:
                                  id: itemName
                                  text: root.col_data[1]
                                  text_size: self.size
                              Label:
                                  text: "State Code"
                                  text_size: self.size
                                  valign: 'middle'
                              TextInput:
                                  id: itemCode
                                  text: root.col_data[2]
                                  text_size: self.size
                  
                          GridLayout:
                              cols: 2
                              padding: 10, 0, 10, 10
                              spacing: 10, 10
                              row_default_height: '20dp'
                              size_hint: .55, .3
                              pos_hint: {'x': .25, 'y':.65}
                  
                              Button:
                                  size_hint_x: .5
                                  text: "Ok"
                                  on_release:
                                      root.update(root)
                                      root.dismiss()
                  
                              Button:
                                  size_hint_x: .5
                                  text: "Cancel"
                                  on_release: root.dismiss()
                  
                  
                  
                  <Invoice>:
                      BoxLayout:
                          orientation: "vertical"
                          padding : 15, 15
                  
                          BoxLayout:
                              orientation: "vertical"
                              padding : 5, 5
                              size_hint: .6, None
                              pos_hint: {'x': .18,}
                  
                  
                              BoxLayout:
                                  orientation: "horizontal"
                                  padding : 5, 5
                                  spacing: 10, 10
                                  size: 800, 40
                                  size_hint: 1, None
                  
                                  Button:
                                      text: "Show"
                                      size_hint_x: .05
                                      spacing_x: 30
                                      on_press:root.abc()
                  
                          BoxLayout:
                              orientation: "horizontal"
                              size_hint: 1, 1
                  
                              BoxLayout:
                                  orientation: "vertical"
                                  size_hint: .5, 1
                                  padding : 0, 15
                                  spacing: 10, 10
                                  size: 500, 30
                  
                                  GridLayout:
                                      id: invoices
                                      cols: 2
                                      #orientation: "horizontal"
                                      padding : 5, 0
                                      spacing: 10, 0
                                      #size: 500, 30
                                      size_hint: 1, 1
                                      pos: self.pos
                                      size: self.size
                  

                  推荐答案

                  您正在创建一个新的 Invoice 实例,而不是使用现有的实例.

                  You are creating a new Invoice instance instead of using the existing one.

                   Invoice().abc()
                  

                  试试吧:

                  class EditPopup(Popup):
                      mode = StringProperty("")
                      label_rec_id = StringProperty("Id")
                      col_data = ListProperty(["?", "?", "?"])
                      index = NumericProperty(0)
                  
                      def __init__(self, obj, **kwargs):
                          super(EditPopup, self).__init__(**kwargs)
                          self.obj = obj # will need it later...
                  
                      def update(self,obj):
                          #cur.execute("UPDATE `item` SET itemName=?, itemCode=? WHERE itemId=?",
                                  #('Item1', 9999, 11))
                          #con.commit()
                  
                          self.obj.abc()  # was Invoice().abc()
                  

                  这篇关于Python/Kivy:从一个类调用函数到另一个类并在 Python 中显示小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python - 在应用程序中显示 Web 浏览器/iframe 下一篇:在 Kivy 中将图像对象作为按钮背景传递

                  相关文章

                    1. <small id='3YiCR'></small><noframes id='3YiCR'>

                      <i id='3YiCR'><tr id='3YiCR'><dt id='3YiCR'><q id='3YiCR'><span id='3YiCR'><b id='3YiCR'><form id='3YiCR'><ins id='3YiCR'></ins><ul id='3YiCR'></ul><sub id='3YiCR'></sub></form><legend id='3YiCR'></legend><bdo id='3YiCR'><pre id='3YiCR'><center id='3YiCR'></center></pre></bdo></b><th id='3YiCR'></th></span></q></dt></tr></i><div id='3YiCR'><tfoot id='3YiCR'></tfoot><dl id='3YiCR'><fieldset id='3YiCR'></fieldset></dl></div>

                    2. <legend id='3YiCR'><style id='3YiCR'><dir id='3YiCR'><q id='3YiCR'></q></dir></style></legend>
                      <tfoot id='3YiCR'></tfoot>
                        <bdo id='3YiCR'></bdo><ul id='3YiCR'></ul>