按scrollview中的按钮切换到另一个屏幕[Kivy]

2024-10-01 07:44:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有scrollview和scrollview中的我构建按钮。一切都在first_screen。我想按ฺButtonbuy切换到second_screen。我尝试过使用button.bind(按=…),但仍然无法切换到另一个屏幕。有人知道怎么做吗

这是我的密码

白痴

***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    

Tags: textnameselfaddsizettfwidgetscreen
1条回答
网友
1楼 · 发布于 2024-10-01 07:44:03

基本上,它应该是这样工作的:

Buttonbuy=Button(text="ซสินค้า",font_name="WareeSans.ttf", on_press=self.change_screen)

然后,您需要在第一个屏幕中定义自定义方法更改屏幕:

def change_screen(self, instance):
    self.parent.current = "second_screen" 

相关问题 更多 >