Reputation: 11
I am currently making a Python app using Kivy and KivyMD for UI. The general idea of my app is for the user to press a button and a QR code image will appear in the app window. If the user presses the button again, a new QR code image should appear. I am trying to use the Kivy reload()
function to update the QR code image but it only updates the image in the directory and not in the app window.
Kivy Builder load string:
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 Code:
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()
Upvotes: 0
Views: 873
Reputation: 244252
image
is different from qr
, the first one is created each time the b32Keys function is invoked and the second is the one shown, so reloading image
does not reload qr
. So the solution is to access qr
directly, in this case you must use the root and 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()
Upvotes: 0