我有一个滚动视图,我在滚动视图中建立按钮。这一切都在 first_screen
. 我想按 ฺButtonbuy
改为 second_screen
. 我已经尝试使用button.bind(on_press=......),但它仍然没有改变到另一个屏幕。有谁知道该怎么做?
下面是我的代码
.py
***In Screen1***
def create_scrollview(self,dt):
s="%s.jpg"
for i in range(15):
picdi=s % (i+1)
self.ids.container_y.add_widget(Image(source=picdi))
detail=GridLayout(rows=4)
for x in myresult:
Labels=Label(text="Slot: ")
detail.add_widget(Labels)
Labeln=Label(text="Name: ")
detail.add_widget(Labeln)
Labelp=Label(text="Price: ")
detail.add_widget(Labelp)
Buttonbox=BoxLayout(orientation='horizontal')
Buttonadd=Button(text="เพิ่มสินค้าลงในรถเข็น",font_name="WareeSans.ttf")
Buttonbox.add_widget(Buttonadd)
#This Button(Buttonbuy) is what I want to change to another screen
Buttonbuy=Button(text="ซื้อสินค้า",font_name="WareeSans.ttf")
#Buttonbuy.bind()
Buttonbox.add_widget(Buttonbuy)
detail.add_widget(Buttonbox)
self.ids.container_y.add_widget(detail)
.kv
<First>
canvas.before:
Rectangle:
source: "red.jpg"
pos: self.pos
size: self.size
BoxLayout:
orientation:'vertical'
Label:
text:'1.กรุณาเลือกสินค้า'
font_name:"WareeSans.ttf"
size_hint_y:.2
font_size:30
ScrollView
GridLayout:
id:container_y
size_hint_y:None
cols:2
spacing:[0,5]
row_default_height:root.height*0.2
row_default_width:root.width*0.2
height:self.minimum_height
width:self.minimum_width
Button:
size_hint_y:.3
text:'รถเข็น'
font_name:"WareeSans.ttf"
<Second>
BoxLayout:
orientation:'vertical'
Label:
text:'2.ตรวจสอบสินค้า'
font_name:"WareeSans.ttf"
size_hint_y:.2
font_size:30
Label:
<UserManager>:
id:user_manager
first_screen:first_screen
second_screen:second_screen
First:
id:first_screen
name:'first_screen'
manager:user_manager
Second:
id:second_screen
name:'second_screen'
manager:user_manager
基本上应该是这样工作的。
Buttonbuy=Button(text="ซสินค้า",font_name="WareeSans.ttf", on_press=self.change_screen)
然后你需要在你的第一个屏幕中定义自定义方法change_screen。
def change_screen(self, instance):
self.parent.current = "second_screen"