[英]Using ScreenManager with separate py and kv files for each screen
我正在制作一个计算器应用程序,我希望能够为每个屏幕使用单独的 kv 和 py 文件,我没有找到任何关于此的文档或教程,我找到的唯一答案没有给出代码示例所以我不确定如何实现它。 此外,calc.kv 上的按钮通过在 root 中引用它来向 calc.py 发送回调,这样可能会导致问题,因为 root 是 screenmanager
如果您尝试从工具栏中打开计算器,它将返回此错误消息:
screen = self.get_screen(value)
File "C:\Python38\lib\site-packages\kivy\uix\screenmanager.py", line 1071, in get_screen
raise ScreenManagerException('No Screen with name "%s".' % name)
kivy.uix.screenmanager.ScreenManagerException: No Screen with name "calc".
这是代码:
计算.py
from logging import root
from kivymd.app import MDApp
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.properties import StringProperty
from kivy.lang import Builder
prev = ""
expression = ""
prox = ""
class calc(Widget):
pass
out = StringProperty('')
Window.size = (420, 200)
Window.minimum_width, Window.minimum_height = Window.size
def my_callback(self, inpt):
global prev
global expression
global prox
if inpt == "=":
prox = str(eval(expression))
self.out = prox
expression = ""
prev = prox
elif inpt == "ac":
expression = ""
prev = ""
prox = ""
self.out = expression
else:
expression = prev + inpt
prev = expression
self.out = expression
class calcApp(MDApp):
def build(self):
def press(text):
print("called: ", text)
return calc()
if __name__ == '__main__':
calcApp().run()
计算.kv
#:kivy 1.0.9
<calc>:
BoxLayout:
orientation: 'vertical'
cols: 2
size: root.width, root.height
MDLabel:
id: 'output'
text: str(root.out)
size_hint: 1, 0.2
halign: 'center'
GridLayout:
cols: 5
MDFlatButton:
text: '1'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '2'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '3'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '-'
on_press: root.my_callback(self.text)
MDFlatButton:
text: 'ac'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '4'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '5'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '6'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '+'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '('
on_press: root.my_callback(self.text)
MDFlatButton:
text: '7'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '8'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '9'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '*'
on_press: root.my_callback(self.text)
MDFlatButton:
text: ')'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '0'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '.'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '/'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '**'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '='
on_press: root.my_callback(self.text)
主文件
#:kivy 1.0.9
#: include calc.kv
NavigationLayout:
id: nav_layout
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: app.title
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
ScreenManager:
id: screen_manager
calculator:
MDNavigationDrawer:
id: nav_drawer
BoxLayout:
orientation: 'vertical'
padding: '8dp'
spacing: '8dp'
Image:
size_hint: None, None
size: '280dp', "200dp"
source: "logo.png"
ScrollView:
MDList:
OneLineIconListItem:
text: "calculadora"
on_release:
screen_manager.current = 'calc'
nav_drawer.set_state()
IconLeftWidget:
icon: 'view-dashboard'
主文件
import calc
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
class ScreenManager(ScreenManager):
pass
class MyApp(MDApp):
def build(self):
self.title = "calculadora"
self.theme_cls.primary_palette = "Green"
return Builder.load_file("main.kv")
MyApp().run()
提前致谢。
您需要为 calc.kv 文件中的<calc>
命名,而不是从Widget
class 继承,而是从 calc.py 文件中的Screen
class 继承。 之后,您需要使用Builder.load_file("calc.kv")
在 your.kv 中添加calc()
Screen
,如on_release
calc.py (只需在此处将 inheritance 从Widget
更改为Screen
)
from logging import root
from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen
from kivy.core.window import Window
from kivy.properties import StringProperty
from kivy.lang import Builder
prev = ""
expression = ""
prox = ""
class calc(Screen):
out = StringProperty('')
Window.size = (420, 200)
Window.minimum_width, Window.minimum_height = Window.size
def my_callback(self, inpt):
global prev
global expression
global prox
if inpt == "=":
prox = str(eval(expression))
self.out = prox
expression = ""
prev = prox
elif inpt == "ac":
expression = ""
prev = ""
prox = ""
self.out = expression
else:
expression = prev + inpt
prev = expression
self.out = expression
class calcApp(MDApp):
def build(self):
def press(text):
print("called: ", text)
return calc()
if __name__ == '__main__':
calcApp().run()
calc.kv (只需给 class 起一个名称以供参考)
#:kivy 1.0.9
<calc>:
name: 'calc'
BoxLayout:
orientation: 'vertical'
cols: 2
size: root.width, root.height
MDLabel:
id: 'output'
text: str(root.out)
size_hint: 1, 0.2
halign: 'center'
GridLayout:
cols: 5
MDFlatButton:
text: '1'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '2'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '3'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '-'
on_press: root.my_callback(self.text)
MDFlatButton:
text: 'ac'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '4'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '5'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '6'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '+'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '('
on_press: root.my_callback(self.text)
MDFlatButton:
text: '7'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '8'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '9'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '*'
on_press: root.my_callback(self.text)
MDFlatButton:
text: ')'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '0'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '.'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '/'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '**'
on_press: root.my_callback(self.text)
MDFlatButton:
text: '='
on_press: root.my_callback(self.text)
main.py (只需在开始时加载 calc.kv)
import calc
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
Builder.load_file("calc.kv")
class MyApp(MDApp):
def build(self):
self.title = "calculadora"
self.theme_cls.primary_palette = "Green"
return Builder.load_file("main.kv")
MyApp().run()
main.kv (只需导入 calc class,并将其 object 添加到管理器中)
#:kivy 1.0.9
#:import calc calc.calc
NavigationLayout:
id: nav_layout
ScreenManager:
Screen:
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: app.title
elevation: 10
left_action_items: [['menu', lambda x: nav_drawer.set_state()]]
ScreenManager:
id: screen_manager
calculator:
MDNavigationDrawer:
id: nav_drawer
BoxLayout:
orientation: 'vertical'
padding: '8dp'
spacing: '8dp'
Image:
size_hint: None, None
size: '280dp', "200dp"
source: "logo.png"
ScrollView:
MDList:
OneLineIconListItem:
text: "calculadora"
on_release:
screen_manager.add_widget(calc())
screen_manager.current = 'calc'
nav_drawer.set_state()
IconLeftWidget:
icon: 'view-dashboard'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.