所以我只是使用 kivy 和 kivymd 在 python 中编写了一个计算器应用程序,并且在使用这个“选项卡”功能时我 陷入这个问题,我很长时间都无法修复,下面是代码和错误字符串;
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.label import MDLabel
from kivymd.uix.appbar import MDTopAppBar
from kivy.metrics import dp
KV = '''
MDScreen:
MDBoxLayout:
orientation: 'vertical'
MDTopAppBar:
title: "Calculator App"
right_action_items: [["theme-light-dark", lambda x: app.toggle_theme()]]
MDTabs:
id: tabs
allow_stretch: True
pos_hint: {"center_x": .5, "center_y": .5}
MDTabsItem:
text: "Calculator"
icon: "calculator"
MDBoxLayout:
orientation: 'vertical'
padding: dp(10)
MDLabel:
text: "Basic Calculator UI"
halign: 'center'
# Add your calculator UI components here
MDTabsItem:
text: "Converters"
icon: "ruler"
MDBoxLayout:
orientation: 'vertical'
padding: dp(10)
MDLabel:
text: "Converters UI"
halign: 'center'
# Add your converters UI components here
'''
class CalculatorApp(MDApp):
def build(self):
return Builder.load_string(KV)
def toggle_theme(self):
if self.theme_cls.theme_style == "Light":
self.theme_cls.theme_style = "Dark"
else:
self.theme_cls.theme_style = "Light"
CalculatorApp().run()
**错误:** from kivymd.uix.tab import MDTabs # 正确导入 ImportError:无法从“kivymd.uix.tab”导入名称“MDTabs”(C:\ Python 3.9 \ lib \ site-packages \ kivymd \ uix ab_init_.py)
我尝试参考kivymd 2.0.0修复导入。选项卡的文档,但没有真正起作用
我认为解决方案打印在错误消息中:
from kivymd.uix.tab import MDTabs # Correct import
用它代替您的代码:
from kivymd.uix.tab import MDTabsBase