使用来自另一个屏幕的条目更新TextInput字段



嗨,我有一个关于在Kivy传递变量的问题。我的应用程序要做的是:

有一个TextInput字段,用于项的名称。但是,我有一个按钮,当我点击时,我可以扫描QR, QR被翻译成文本,TextInput字段填充了更新的QR翻译。

还有Screen Manager和Sockets。

我怎样才能做到这一点?

我正在努力的主要问题是当QR被翻译时,它被保存为自我。QrScreen中的代码。因为我想更新主屏幕中的TextField,所以主屏幕需要更新self。代号来自"翻译后的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()

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'

还没有测试过这段代码,但是使用idsScreenManagerget_screen()方法应该允许您更新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`

相关内容

  • 没有找到相关文章

最新更新