【发布时间】:2021-01-01 18:57:53
【问题描述】:
我正在制作一个计算器应用程序,我希望能够为每个屏幕使用单独的 kv 和 py 文件,我还没有找到任何关于此的文档或教程,the only answer I have found 没有给出代码示例所以我不确定如何实现它。此外,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".
代码如下:
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()
calc.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)
main.kv
#: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'
main.py
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定义任何Screen。