我的应用程序需要在第一个屏幕中识别用户才能转到第二个屏幕。如果一个按钮仅在textinput在字典中时才有效? 此按钮应位于屏幕上:“用户”
from kivy.app import App
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
import time
import random
'''
A DICTIONARY AS AN EXAMPLE
'''
dic = {}
dic['a'] = 2
class Mapa(Screen):
pass
class Calendario(Screen):
pass
class Professores(Screen):
pass
class Sair(Screen):
pass
class Usuario(Screen):
pass
class MyScreenManager(ScreenManager):
pass
root_widget = Builder.load_string('''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
MyScreenManager:
transition: FadeTransition()
User:
Calendario:
Mapa:
Professores:
Sair:
<User>:
a: _a
name: 'User'
FloatLayout:
Label:
text: 'User'
font_size: 50
size_hint_y: 1.7
size_hint_x: .95
GridLayout:
size_hint_y: 0.08
size_hint_x: .6
height: 80
pos_hint: {'center_x': .5, 'center_y':.6}
cols: 2
Label:
text: "Login"
font_size: 30
TextInput:
id: _a
font_size: 30
multline: False
Button:
text: 'go'
font_size: 30
size_hint_y: .1
size_hint_x: .1
pos_hint: {'center_x': .5, 'center_y':.4}
on_release: app.root.current = 'Calendario'
#if _a in dic:
#on_release: app.root.current = 'Calendario'
<Calendario>:
name: 'Calendario'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Calendario'
font_size: 30
size_hint: [1,.2]
BoxLayout:
BoxLayout:
Button:
text: 'Mapa'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Mapa'
Button:
text: 'Calendario'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Calendario'
Button:
text: 'Professores'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Professores'
Button:
text: 'Sair'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Usuario'
<Mapa>:
name: 'Mapa'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Mapa'
font_size: 30
size_hint: [1,.2]
BoxLayout:
BoxLayout:
Button:
text: 'Mapa'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Mapa'
Button:
text: 'Calendario'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Calendario'
Button:
text: 'Professores'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Professores'
Button:
text: 'Sair'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Usuario'
<Professores>:
name: 'Professores'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Professores'
font_size: 30
size_hint: [1,.2]
BoxLayout:
BoxLayout:
Button:
text: 'Mapa'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Mapa'
Button:
text: 'Calendario'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Calendario'
Button:
text: 'Professores'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Professores'
Button:
text: 'Sair'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Usuario'
<Sair>:
name: 'Sair'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Sair'
font_size: 30
size_hint: [1,.2]
BoxLayout:
BoxLayout:
Button:
text: 'Mapa'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Mapa'
Button:
text: 'Calendario'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Calendario'
Button:
text: 'Professores'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Professores'
Button:
text: 'Sair'
font_size: 20
size_hint: [1,.2]
on_release: app.root.current = 'Usuario'
''')
class ScreenManagerApp(App):
def build(self):
return root_widget
ScreenManagerApp().run()
答案 0 :(得分:0)
使用:
#: import dic main.dic
其中main是.py文件的名称
然后在你的kv文件的正文中你应该能够做到:
on_release: if dic['a']==2: app.root.current = 'Calendario'
答案 1 :(得分:0)
是的,有一种(而且丑陋)的方式如何做到这一点,因为这样的奶奶是多少奶奶回答的。丑陋是因为你定义你的dict(作为一个全局)的方式,所以没有很好的方法如何使用它。
Kivy在另一边有一个主要的课程 - App
,如果你没有直接用runTouchApp()
进行艰苦的训练,它会在每个应用程序中使用,所以对你来说最好的是得到那个
dic = {}
dic['a'] = 2
从您的全局空间并将其放在它所属的位置,或使用更好的全局方式,即get_running_app
,并将这些行放在App
类中__init__
或作为类变量
然后在kv +中使用app.<variable>
访问它的最简单方法如果将get_running_app()
与App
连接在一起并使[Setup]
PrivilegesRequiredOverridesAllowed=dialog
类处理通信,则可以很好地与其他类一起使用。