嗨,我有一个关于在 Kivy 中传递变量的问题。所以我的应用程序正在尝试做的是:
有一个用于项目名称的 TextInput 字段。但是,我有一个按钮,当我单击时,我可以扫描 QR,QR 被翻译成文本,TextInput 字段填充了更新的 QR 翻译。
我也屏幕管理器和套接字。
我怎样才能做到这一点?
我正在努力的主要问题是当 QR 被翻译时,它在 QrScreen 中保存为 self.codename。由于我要更新 MainScreen 中的 TextField,因此 MainScreen 需要将 self.codename 从 "" 更新为翻译后的 QR。这是我不明白的过程。
这是代码:
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()
我的.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'