【问题标题】:Kivy reload() image not updating in app windowKivy reload() 图像未在应用程序窗口中更新
【发布时间】:2019-10-24 04:53:28
【问题描述】:

我目前正在使用 Kivy 和 KivyMD 为 UI 制作 Python 应用程序。我的应用程序的总体思路是让用户按下一个按钮,一个二维码图像就会出现在应用程序窗口中。如果用户再次按下按钮,应该会出现一个新的二维码图像。我正在尝试使用 Kivy reload() 函数更新二维码图像,但它只更新目录中的图像,而不是应用程序窗口中的图像。

Kivy Builder 加载字符串:

 Window.size = (320, 500)

 main_kv = """

 BoxLayout:
     orientation: 'vertical'
      size_hint_y: None
      height: self.minimum_height
      spacing: dp(10)
 MDLabel:
      font_name: 'Roboto-Italic'
      theme_text_color: 'Primary'
      text: "Public Key:"
      halign: 'center'
      pos_hint: {'center_x': .5, 'center_y': .90}
      font_size: 20
 Image:
      id:qr
      source: 'qr.jpg'
      size: self.texture_size
 MDLabel:
      font_name: 'Roboto-Italic'
      theme_text_color: 'Primary'
      text: "Private Key:"
      halign: 'center'
      pos_hint: {'center_x': .5, 'center_y': .50}
      font_size: 20

 MDRectangleFlatIconButton:
      text: "Generate Keys"
      icon: 'polymer'
      opposite_colors: True
      pos_hint: {'center_x': .5 , 'center_y': .08}
      elevation: 10
      on_press: app.b32Keys()
      opposite_colors: True


"""

Python 代码:

class KnixBTC(App):
    theme_cls = ThemeManager()
    theme_cls.primary_palette = 'DeepPurple'
    theme_cls.accent_palette = 'Orange'
    theme_cls.theme_style = 'Dark'

    def build(self):
        self.main_widget = Builder.load_string(main_kv)
        return self.main_widget

    def b32Keys(self):
        image = Image(source='qr.jpg', nocache=True)
        privateKey = PrivateKey.random()
        private = privateKey.wif(compressed=True)
        publicKey = privateKey.to_public()
        bech32 = publicKey.to_address('P2WPKH')
        genQR = qrcode.make(bech32)
        genQR.save("qr.jpg")
        image.reload()


if __name__ == "__main__":
    KnixBTC().run()

【问题讨论】:

  • 什么时候调用 b32Keys?
  • 尝试将nocache=True 添加到您的Image() 通话中。
  • 我想我没有将我的按钮代码放在原始帖子中,但我在按钮代码中调用了 b32Keys()。我把 nocache=True 放在“source = 'qr.pg'”之后,它仍然不在应用程序窗口中。我只是用我的按钮更新我的原始代码来调用 b32Keys()

标签: python kivy kivy-language


【解决方案1】:

imageqr 不同,第一个是在每次调用 b32Keys 函数时创建的,第二个是显示的,因此重新加载 image 不会重新加载 qr。所以解决方法是直接访问qr,这种情况下必须使用root和id:

def b32Keys(self):
    privateKey = PrivateKey.random()
    private = privateKey.wif(compressed=True)
    publicKey = privateKey.to_public()
    bech32 = publicKey.to_address('P2WPKH')
    genQR = qrcode.make(bech32)
    genQR.save("qr.jpg")
    self.main_widget.ids.qr.reload()

【讨论】:

  • 太棒了!这也正是我想要的方式!!!非常感谢您的帮助!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多