【问题标题】:Press button in scrollview to change to another screen [Kivy]在滚动视图中按下按钮以切换到另一个屏幕 [Kivy]
【发布时间】:2020-05-07 20:45:31
【问题描述】:

我有滚动视图并在滚动视图中构建按钮。这一切都在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    

【问题讨论】:

    标签: python python-3.x user-interface kivy kivy-language


    【解决方案1】:

    基本上它应该这样工作:

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

    然后你需要在你的第一个屏幕中定义自定义方法change_screen:

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

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多