我是新手。我正在尝试制作一个包含多个屏幕的应用程序。每个屏幕都包含一个布局和一些小部件。我想从按钮上获得通常的功能,而不仅仅是切换屏幕。 例如,我想使用按钮的on_press方法更改标签的文本。 我怎样才能做到这一点? 这是我的.py代码:
import kivy
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty, NumericProperty
from kivy.animation import Animation
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class ScreenOne(Screen):
flo = ObjectProperty(None)
def b1p(self, instance):
global psss
psss = "1"+psss
print("okok")
print(Manager.__dict__)
print(root.ids.screen_one.ids.label1.text)
## print(ScreenOne.flo.button1.text)
instance.text = "hk"
class ScreenTwo(Screen):
pass
class ScreenThree(Screen):
pass
global psss
psss= ""
class Manager(ScreenManager):
## font_sizte = NumericProperty(20)
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
screen_three = ObjectProperty(None)
def b1p(self):
global psss
psss = "1"+psss
print("okok")
## self.button1.text = "hi"
class ScreensApp(App):
font_size_pssscreen = 30
shx_pssscreen = 0.1
shy_pssscreen = 0.15
def build (self):
return Manager()
if __name__ == "__main__":
ScreensApp().run()
和.kv代码:
#:kivy 1.8.0
<ScreenOne>:
flo : flo
FloatLayout:
id:flo
label1 : label1
button1:button1
button2:button2
button3:button3
button4:button4
button5:button5
button6:button6
button7:button7
button8:button8
button9:button9
Button:
font_size :app.font_size_pssscreen
color:0.3,0.6,0.7,1
size_hint:app.shx_pssscreen,app.shy_pssscreen
id:button1
pos_hint:{"x":.3,"top":.75}
back_color:.1,.3,.6,1
text:"1"
on_press:root.b1p(root.manager)
#on_press: self.text +=root
Button:
id:button2
font_size :app.font_size_pssscreen
color:0.3,0.6,0.7,1
size_hint:app.shx_pssscreen,app.shy_pssscreen
pos_hint:{"x":0.45,"top":.75}
text:"2"
on_press:button2.text:"jio"
Button:
id:button3
font_size :app.font_size_pssscreen
color:0.3,0.6,0.7,1
size_hint:app.shx_pssscreen,app.shy_pssscreen
pos_hint:{"x":0.6,"top":.75}
text:"3"
Button:
id:button4
font_size :app.font_size_pssscreen
color:0.3,0.6,0.7,1
size_hint:app.shx_pssscreen,app.shy_pssscreen
pos_hint:{"x":0.3,"top":.55}
text:"4"
on_press:root.manager.current = 'screen2'
Button:
id:button5
font_size :app.font_size_pssscreen
color:0.3,0.6,0.7,1
size_hint:app.shx_pssscreen,app.shy_pssscreen
pos_hint:{"x":0.45,"top":.55}
text:"5"
Button:
id:button6
font_size :app.font_size_pssscreen
color:0.3,0.6,0.7,1
size_hint:app.shx_pssscreen,app.shy_pssscreen
pos_hint:{"x":0.6,"top":.55}
text:"6"
Button:
id:button7
font_size :app.font_size_pssscreen
color:0.3,0.6,0.7,1
size_hint:app.shx_pssscreen,app.shy_pssscreen
pos_hint:{"x":0.3,"top":.35}
text:"7"
Button:
id:button8
font_size :app.font_size_pssscreen
color:0.3,0.6,0.7,1
size_hint:app.shx_pssscreen,app.shy_pssscreen
pos_hint:{"x":0.45,"top":.35}
text:"8"
Button:
id:button9
font_size :app.font_size_pssscreen
color:0.3,0.6,0.7,1
size_hint:app.shx_pssscreen,app.shy_pssscreen
pos_hint:{"x":0.6,"top":.35}
text:"9"
Label:
id:label1
font_size :app.font_size_pssscreen
color:0.3,0.6,0.7,1
size_hint:app.shx_pssscreen,app.shy_pssscreen
pos_hint:{"x":0.0,"top":1.3}
text:"w"
<ScreenTwo>:
Button:
text:"hi"
<ScreenThree>:
Button:
text:"hi"
<Manager>:
id: screen_manager
screen_one: screen_one
screen_two:screen_two
screen_three:screen_three
ScreenOne:
id:screen_one
name: 'screen1'
manager:screen_manager
ScreenTwo:
id:screen_two
name: 'screen2'
manager:screen_manager
ScreenThree:
id:screen_three
name: 'screen3'
manager:screen_manager
答案 0 :(得分:0)
在b1p()
的{{1}}方法中,只需添加一个调用即可更改文本:
ScreenOne