Hi I have a question about passing variables in Kivy. So what my app is trying to do is:
Have a TextInput field that for a name of an item. However, I have a button where when I click, I can scan the QR, QR is translated into a text and the TextInput field is filled with updated with the QR translation.
I also Screen Manager and Sockets.
How can I achieve this?
The main problem I am struggling is when the QR is translated, it is saved as self.codename in the QrScreen. Since I want to update the TextField in the MainScreen, the MainScreen needs to update the self.codename from "" to the translated QR. This is the process that I don't understand.
here is the code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
# Import Package that deals with socket
import socket_client
# Import QR Reader
import qr_reader
# from android.permissions import request_permissions, Permission
# request_permissions([
# Permission.CAMERA,
# Permission.WRITE_EXTERNAL_STORAGE,
# Permission.READ_EXTERNAL_STORAGE
# ])
class MainScreen(Screen):
def update_name(self, new_qr):
print(' Iam updating this name ' new_qr)
def turn_camera_on(self):
print("turning camera on...")
def send_message(self, _):
message = _
print(message)
if message:
socket_client.send(message)
class QrScreen(Screen):
camera = ObjectProperty(None)
def capture_image(self):
texture = self.camera.texture
size = texture.size
pixels = texture.pixels
self.codename = qr_reader.convert_qr(size, pixels)
print(self.codename)
# update the text in the main page
# switch screen to main page
def send_message(self, _):
message = _
print(message)
if message:
socket_client.send(message)
class SettingsScreen(Screen):
ipadd = ObjectProperty(None)
port = ObjectProperty(None)
username = ObjectProperty(None)
def connect(self):
print(f"joining {self.ipadd.text} | {self.port.text} AS {self.username.text}")
# Get information for sockets client
port = int(self.port.text)
ip = self.ipadd.text
username = self.username.text
if not socket_client.connect(ip, port, username, show_error):
return
class MyApp(App):
# Initiate the variable for codename
codename = ObjectProperty(None)
def build(self):
self.codename = ""
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MainScreen(name='mainpage'))
sm.add_widget(QrScreen(name='qrpage'))
sm.add_widget(SettingsScreen(name='settings'))
return sm
def show_error(message):
pass
if __name__ == '__main__':
theapp = MyApp()
theapp.run()
my.kv:
<MainScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 2
codename: codename
Label:
text: "Code Name"
TextInput:
id: codename
multiline: False
Button:
text: 'Scan QR'
on_press: root.manager.current = 'qrpage'
on_press: root.turn_camera_on()
Button:
text: 'Go'
on_press: root.send_message("Go")
Button:
text: 'Stop'
on_press: root.send_message("Stop")
Button:
text: 'Setting'
on_press: root.manager.current = 'settings'
<QrScreen>:
camera: camera
GridLayout:
cols: 1
Camera:
id: camera
resolution: (640,480)
play: False
GridLayout:
cols: 2
ToggleButton:
text: 'Play'
on_press: camera.play = not camera.play
Button:
text: 'Capture'
on_press: root.capture_image()
Button:
text: 'Back'
on_press: root.manager.current = 'mainpage'
<SettingsScreen>:
ipadd: ipadd
port: port
username: username
GridLayout:
cols: 1
GridLayout:
cols:2
Label:
text: "IP Address: "
TextInput:
id: ipadd
text: "192.168.1.65"
multiline: False
Label:
text: "Port: "
TextInput:
id: port
text: "1234"
multiline: False
Label:
text: "Username: "
TextInput:
id: username
text: "User"
multiline: False
Button:
text: 'Join'
on_press: root.connect()
Button:
text: 'Back'
on_press: root.manager.current = 'mainpage'
CodePudding user response:
Haven't tested this code, but using ids
and the get_screen()
method of ScreenManager
should allow you to update the Textinput
:
def capture_image(self):
texture = self.camera.texture
size = texture.size
pixels = texture.pixels
self.codename = qr_reader.convert_qr(size, pixels)
print(self.codename)
# update the text in the main page
main_screen = self.manager.get_screen('mainpage')
main_screen.ids.codename.text = self.codename
# switch screen to main page
self.manager.current = `mainpage`