我正在制作一个kivy应用程序,但遇到了无法更新标签的问题。我不知道从哪里去,但我希望日期值由Class ThirdMenu中的标签显示。这是我所有的代码,有点混乱。我是新手,还没有完全掌握所有概念,因此很可能会出现很多错误。
这是带有我要更改标签的课程:
class ThirdMenu(Screen, FloatLayout, EventDispatcher):
theme_cls = ThemeManager()
pickers = None
previous_date = ''
a = NumericProperty(0)
def __init__(self, **kwargs):
super(ThirdMenu, self).__init__(**kwargs)
global Date
#Labels
app = App.get_running_app()
app.D = Label(text=str(app.a),bold = True,text_size=(None,None),font_size="30sp",pos_hint={'center_x': 0.50, 'y': .8},size_hint_y=None,size = self.size,color=(1, 0, 0, 1))
self.add_widget(app.D)
#Buttons
FBBtn = Button(text ='Back to menu', size_hint =(1, .15), pos_hint ={'x': 0, 'y': 0.85})
self.add_widget(FBBtn)
FBBtn.bind(on_press = self.change_screen_menu)
Builder.load_string(KV)
self.pickers = Factory.Pickers()
self.add_widget(self.pickers)
#Screen manager / changing screens route
def change_screen_menu(self, *args):
self.manager.transition = SlideTransition(direction ='down')
self.manager.current = 'menu'
print('Screen is: ' + str(self.manager.current))
def on_a(self, instance, value):
app = App.get_running_app()
app.D.text = str(a)
print (a)
在此类中加载的KV字符串:
KV = """
<Pickers@Screen>
name: 'pickers'
BoxLayout:
orientation: 'vertical'
spacing: dp(20)
pos_hint: {'center_x': .5, 'center_y': .5}
size_hint_y: None
height: self.minimum_height
MDRaisedButton:
text: "Open date picker"
pos_hint: {'center_x': .5}
opposite_colors: True
on_release: app.show_example_date_picker()
MDLabel:
id: date_picker_label
theme_text_color: 'Primary'
halign: 'center'
Label:
id: Date
theme_text_color: 'Primary'
halign: 'center'
BoxLayout:
size_hint: None, None
size: self.minimum_size
pos_hint: {'center_x': .5}
Label:
theme_text_color: 'Primary'
text: "Start on previous date"
size_hint_x: None
width: self.texture_size[0]
color: 0, 0, 0, 0
MDCheckbox:
id: date_picker_use_previous_date"""
然后是我的主类,它带有更改日期值的功能:
class MyApp(App):
theme_cls = ThemeManager()
pickers = None
previous_date = ''
a = StringProperty(0)
def build(self):
Window.size = (750,1334)
Window.clearcolor = (1, 1, 1, 1)
#set up of screen manager
sm = ScreenManager()
#Adding the different screens
sm.add_widget(MainMenu(name='menu'))
sm.add_widget(MainApp(name='main'))
sm.add_widget(SecondMenu(name='second'))
sm.add_widget(ThirdMenu(name='third'))
sm.add_widget(FourthMenu(name='fourth'))
sm.add_widget(TinaMenu(name='tinamenu'))
sm.add_widget(JohnMenu(name='johnmenu'))
#Activating the screen manager
return sm
def show_example_date_picker(self, *args):
self.pickers = Factory.Pickers()
if self.pickers.ids.date_picker_use_previous_date.active:
pd = self.previous_date
try:
MDDatePicker(self.set_previous_date, pd.year, pd.month, pd.day).open()
except AttributeError:
MDDatePicker(self.set_previous_date).open()
else:
MDDatePicker(self.set_previous_date).open()
def set_previous_date(self, date_obj):
global a
app = App.get_running_app()
self.previous_date = date_obj
self.pickers.ids.date_picker_label.text = str(date_obj)
app.a = self.pickers.ids.date_picker_label.text
print(app.a)
ThirdMenu.text = str(app.a)
非常感谢您的阅读。
答案 0 :(得分:0)
只要具有标签ID,就可以在主文件上更改其值。
(...)
Label:
id: Date
theme_text_color: 'Primary'
halign: 'center'
(...)
在main.py上 (...)
def change_date(newValue):
self.Date = newValue
(...)
答案 1 :(得分:0)
代码中发生了几件奇怪的事情:
ThirdMenu
类不需要扩展所有这些类。只需扩展Screen
就足够了,因为Screen
已经是RelativeLayout
和EventDispatcher
。Pickers
类扩展了Screen
,但是您没有将其用作Screen
。您可以改为扩展RelativeLayout
self.pickers = Factory.Pickers()
。这为您提供了Pickers
类的两个不同的不相关实例。对在Pickers
类中创建的MyApp
实例进行更改将不会对Pickers
中显示的ThirdMenu
实例产生影响。您可以使用与下面的第5点类似的方法来访问Pickers
方法中的show_example_date_picker()
的正确实例。set_previous_date()
方法中,您使用app = App.get_running_app()
,但在该方法中,self
是应用程序。最后,在您的set_previous_date()
方法中,您可以使用设置标签文本
self.root.get_screen('third')。pickers.ids.Date.text = str(date_obj)
这将访问root
的{{1}}的{{1}},然后获得名称为App
(ScreenManager
的{{1}} ),然后访问Screen
的{{1}}属性,然后访问third
的{{1}}字典,从中它访问ThirdScreen
({{1} }中的{}},并将该pickers
的{{1}}属性设置为选择的日期。