python - 我无法从 kivymd 的另一个屏幕获取文本字段的值
问题描述
screen_helper = """
#:import utils kivy.utils
ScreenManager:
id:screen_manager
MenuScreen:
RulesScreen:
NumberPlayers:
ProfileScreen:
PlayersScreen:
SettingsScreen:
<MenuScreen>:
name: 'menu'
MDRectangleFlatButton:
text: 'Start'
pos_hint: {'center_x':0.5,'center_y':0.6}
on_press:
root.manager.current = 'number'
root.manager.transition.direction= "left"
MDRectangleFlatButton:
text: 'Rules'
pos_hint: {'center_x':0.5,'center_y':0.5}
on_press:
root.manager.current = 'Rules'
root.manager.transition.direction= "up"
MDRectangleFlatButton:
text: 'Settings'
pos_hint: {'center_x':0.5,'center_y':0.4}
on_press:
root.manager.current = 'settings'
root.manager.transition.direction= "down"
<NumberPlayers>:
name: 'number'
MDTextField:
id: **numberfield**
hint_text: 'Enter the number of players'
helper_text: 'Max 4 users allowed'
helper_text_mode: 'on_focus'
color_mode: 'custom'
line_color_focus: app.theme_cls.primary_color
icon_right: 'account'
required: True
max_text_length: 4
icon_right_color: app.theme_cls.primary_color
pos_hint:{'center_x': 0.5, 'center_y': 0.5}
size_hint_x:None
width:300
MDRectangleFlatButton:
text: 'Next'
pos_hint:{'center_x': 0.75, 'center_y': 0.2}
on_press:
app.disabling_button(numberfield.text,root)
MDRectangleFlatButton:
text: 'Back'
pos_hint: {'center_x':0.25,'center_y':0.2}
on_press:
root.manager.current = 'menu'
root.manager.transition.direction= "right"
<RulesScreen>:
name: 'Rules'
MDLabel:
text: 'Answer each question with different answers. All the answers will be shuffled randomly and in the end you will have many funny sentences'
halign: 'center'
MDRectangleFlatButton:
text: 'Back'
pos_hint: {'center_x':0.25,'center_y':0.2}
on_press:
root.manager.current = 'menu'
root.manager.transition.direction= "down"
<ProfileScreen>:
name: 'profile'
MDTextField:
max_text_length: 10
helper_text: 'You can enter 10 characters'
helper_text_mode: 'on_focus'
id: profilefield
hint_text: "Enter username for player 1"
icon_right: "account-key"
required: True
max_text_lenght: 12
icon_right_color: app.theme_cls.primary_color
pos_hint:{'center_x': 0.5, 'center_y': 0.5}
size_hint_x:None
width:300
MDRectangleFlatButton:
text: 'Back'
pos_hint: {'center_x':0.25,'center_y':0.2}
on_press:
root.manager.current = 'number'
root.manager.transition.direction= "right"
MDRectangleFlatButton:
text:'Save'
pos_hint:{'center_x': 0.75, 'center_y': 0.2}
on_press:
app.disabling_button2(profilefield.text,root)
<Label>
background_color: (1,1,1,0)
canvas.before:
Color:
rgba: self.background_color
Rectangle:
size: self.size
pos: self.pos
# label text color
color:app.theme_cls.primary_color
<PlayersScreen>:
name: 'players'
MDRectangleFlatButton:
text: 'Back'
pos_hint: {'center_x':0.25,'center_y':0.2}
on_press:
root.manager.current = 'profile'
root.manager.transition.direction= "right"
MDRectangleFlatButton:
text:'Save'
pos_hint:{'center_x': 0.75, 'center_y': 0.2}
on_press:
root.manager.current = 'results'
GridLayout:
cols: 1
pos: 0,0
size: root.width , root.height #take size of entire screen
BoxLayout:
orientation: "vertical"
size: root.width, root.height # fot take entire screen
spacing: 0 # space btween buttons
padding: 130
Label:
text: "Who?"
MDTextField:
id: who
multiline: False
Label:
text: "With whom"
MDTextField:
id: whom
multiline: False
Label:
text: "What does?"
MDTextField:
id: whatdoes
multiline: False
Label:
text: "When?"
MDTextField:
id: when
multiline: False
Label:
text: "Where?"
MDTextField:
id: where
multiline: False
Label:
text: ''
id: result
font_size: 20
<SettingsScreen>:
name: 'settings'
MDSwitch:
pos_hint: {'center_x':0.5,'center_y':0.5}
on_active: app.on_switch_active(self.active)
MDLabel:
text: 'Light Mode'
pos_hint: {'center_x':0.3,'center_y':0.5}
halign: 'center'
font_style: 'Subtitle2'
MDLabel:
text: 'Dark Mode'
pos_hint: {'center_x':1.15,'center_y':0.5}
haligh: 'center'
font_style: 'Subtitle2'
MDRectangleFlatButton:
text:'Go back to main menu'
pos_hint: {'center_x':0.5,'center_y':0.4}
on_press: root.manager.current = 'menu'
"""
from kivymd.app import MDApp
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager
from kivymd.uix.screen import Screen
from kivy.lang import Builder
from helper import screen_helper
from kivymd.uix.textfield import TextInput
class MenuScreen(Screen):
pass
class NumberPlayers(Screen):
pass
class ProfileScreen(Screen):
pass
class RulesScreen(Screen):
pass
class PlayersScreen(Screen):
pass
class SettingsScreen(Screen):
pass
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(ProfileScreen(name='profile'))
sm.add_widget(RulesScreen(name='Rules'))
sm.add_widget(PlayersScreen(name='players'))
sm.add_widget(NumberPlayers(name='number'))
sm.add_widget(SettingsScreen(name='settings'))
class DemoApp(MDApp, NumberPlayers):
def build(self):
self.theme_cls.primary_palette = "Blue"
screen = Screen()
self.username = Builder.load_string(screen_helper)
screen.add_widget(self.username)
return screen
def on_switch_active(self, value):
if value:
self.theme_cls.theme_style = "Dark"
else:
self.theme_cls.theme_style = "Light"
def disabling_button(self, empty, root):
if empty and len(empty) < 5:
root.manager.current = 'profile'
else:
self.disabled = True
def disabling_button2(self, empty, root):
if empty and len(empty) < 11:
root.manager.current = 'players'
root.manager.transition.direction = 'left'
else:
self.disabled = True
这是 main.py 我应该得到一个 id 为 numberfield 的 textfield 的值。我是 kivy 和 kivymd 的新学习者。任何人都可以帮助我在这个文本字段中获得输入吗?我尝试使用 .ids.numberfield.text 方法,但是当我尝试它时出现错误,提示“在 MDApp 中找不到属性 ID”
解决方案
推荐阅读
- gcc - 为什么 nm libc.so 报告没有符号?
- php - 查询字符串值在 Javascript 中不起作用
- c# - 如何在 OnPropertyChanged 中将其作为字符串参数传递时避免硬编码 WPF MVVM 属性名称
- ruby-on-rails - 使用 devise gem 时是否可以为访问者添加一个页面?
- java - java spring Long[](数组)类型
- facebook - Google Ads 中的 Facebook 归因跟踪模板
- django - 为什么可以“多种方式”导入UpdateView
- javascript - 通过单击复选框(Vue.js)切换 div
- php - 提交按钮已被点击
- angular - Angular 6:使用延迟加载在同一页面上加载多个模块