【发布时间】:2020-12-27 16:35:36
【问题描述】:
当用户按下底部的图标时,我正在尝试设置一个逻辑来更改屏幕。我可以使用“测试”按钮更改屏幕,但底部工具栏图标有困难。下面是我的代码。当我按下第二个图标时,它会打印“Hello”,这意味着代码似乎可以工作,但我觉得我缺少连接到不同屏幕的 kivy 功能。 请让我知道我做错了什么。
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder
from kivymd.uix.floatlayout import FloatLayout
from kivymd.uix.textfield import MDTextField
# Builder String
allLayout = '''
ScreenManager:
MainPage:
#List:
#History:
StudentName:
#Support:
#FAQ:
#TermsAndConditions:
#MyAccount:
<Button@MdButton>:
font_size: 20
background_color: 144/235,252/255,144/255,1
size_hint: 0.32, 0.07
<Ticker@MDTextField>:
font_size: 20
hint_text: "Enter student Name"
icon_right: 'database-search'
helper_text: "From the list (2nd icon in bottom bar)"
helper_text_mode: 'on_focus'
size_hint_x : None
width:200
<MainPage>:
name: 'mainpage1'
NavigationLayout:
ScreenManager:
Screen:
BoxLayout:
orientation:'vertical'
MDToolbar:
title: 'Main Page'
elevation: 10
right_action_items: [["dots-vertical", lambda x: x]]
# on_action_button: app.callback(self.icon)
Widget: #if you delete this line... you will spend hours figuring out like before
MDBottomAppBar:
MDToolbar:
icon: "history"
text_color: 0,0,0,0
icon_size: 0.5
type: "bottom"
mode: "end"
left_action_items: [["home", lambda x: x], ["view-list", lambda x: app.listScreen()], ["watch", lambda x: x], ["help", lambda x: x]]
Button:
text:"Class 1"
pos_hint: {'x': 0.025, 'y':.8}
Button:
text:"Class 2"
pos_hint: {'x': 0.345, 'y':.8}
Button:
text:"Class 3"
pos_hint: {'x': 0.665, 'y':.8}
Ticker:
pos_hint: {'x':0.025, 'y': .7}
Button:
text:"Test"
pos_hint: {'x': 0.665, 'y':.6}
on_release:
root.manager.current = 'student'
<StudentName>:
name: 'student'
MDLabel:
text: "Hi"
'''''
class MainPage(Screen):
pass
class StudentName(Screen):
pass
sm = ScreenManager()
sm.add_widget(MainPage(name = 'mainpage1'))
sm.add_widget(StudentName(name = 'student'))
class DemoApp(MDApp):
def listScreen(self):
self.current = StudentName()
print("Hello")
# root.manager.transition.direction = 'left'
def build(self):
self.theme_cls.primary_palette = 'Lime'
self.theme_cls.primary_hue = 'A700'
screen = Screen()
main_code = Builder.load_string(allLayout)
screen.add_widget(main_code)
return screen
DemoApp().run()
【问题讨论】:
标签: python-3.x kivy kivy-language kivymd