如何在 Kivy 中为下一个窗口创建下拉列表

发布于 2025-01-10 23:01:37 字数 2913 浏览 0 评论 0原文

单击第一个屏幕中的按钮后,如何在下一个窗口中创建下拉列表,我尝试启动 kv 文件中的按钮,只是为了在 ModelWindow 屏幕中拥有一个按钮,但下一个问题是 ModelWindow 类中的按钮变量消失了。 lista 方法需要按钮变量才能激活下拉列表

Python 文件

import kivy
from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.button import Button 
from kivy.properties import ObjectProperty 
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.dropdown import DropDown
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label


class ModelWindow(Screen):
    def window(self):
        box = BoxLayout(orientation='vertical')
        label = Label(text='LABEL')
        button = Button(text='Selecione', font_size=30, size_hint_y=0.15, on_release=self.lista)
        box.add_widget(label)
        box.add_widget(button)

        self.dropdown = DropDown()  # Create the dropdown once and keep a reference to it
        self.dropdown.bind(on_select=lambda instance, x: setattr(button, 'text', x))

        for index in range(10):  # create the buttons once
            btn = Button(text='Value %d' % index, size_hint_y=None, height=44,
                            on_release=lambda btn: print(btn.text))  # bind every btn to a print statement
            btn.text = 'Value %d' % index
            btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
            self.dropdown.add_widget(btn)
        return box

    def lista(self, button):
        # dropdown = DropDown()  <---- DON'T NEED THIS
        # button.bind(on_release=self.dropdown.open)  <---- DON'T NEED THIS
        self.dropdown.open(button)  # you need this to open the dropdown
        # print(button.text)
  

class MainWindow(Screen):
    pass

class SecondWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("proj.kv")

class MyMainApp(App):
    def build(self):
        return kv

    

if __name__ == "__main__":
    MyMainApp().run()

Kv 文件

 WindowManager:
    MainWindow:
    SecondWindow:
    ModelWindow:

<ModelWindow>:
    name: "model"
 
<MainWindow>:
    name: "main"
    

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height

        Button:
            text: "Select Model"
            on_press:
                app.root.current = "model" 
                      
        Button:
            text: "Test Model"

        Button:
            text: "Create New Model"
            on_release: 
                app.root.current = "second" 
                root.manager.transition.direction = "left"


<SecondWindow>:
    name: "second"
    Button:
        text: "Go Back"
        on_release:
            app.root.current = "main"
            root.manager.transition.direction = "right"
          

我问是否有人知道如何在下一个窗口中制作下拉列表?我应该把它写在 kv 文件中还是 python 本身?谢谢你

how do I create Dropdown list in the next window after clicking button in the first screen, I tried to initiate the button in the kv file just to have a button in the ModelWindow screen but the next problem is the button variable in ModelWindow class is gone. the button variable is needed for the lista method in order to activate dropdown

Python File

import kivy
from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.button import Button 
from kivy.properties import ObjectProperty 
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.dropdown import DropDown
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label


class ModelWindow(Screen):
    def window(self):
        box = BoxLayout(orientation='vertical')
        label = Label(text='LABEL')
        button = Button(text='Selecione', font_size=30, size_hint_y=0.15, on_release=self.lista)
        box.add_widget(label)
        box.add_widget(button)

        self.dropdown = DropDown()  # Create the dropdown once and keep a reference to it
        self.dropdown.bind(on_select=lambda instance, x: setattr(button, 'text', x))

        for index in range(10):  # create the buttons once
            btn = Button(text='Value %d' % index, size_hint_y=None, height=44,
                            on_release=lambda btn: print(btn.text))  # bind every btn to a print statement
            btn.text = 'Value %d' % index
            btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
            self.dropdown.add_widget(btn)
        return box

    def lista(self, button):
        # dropdown = DropDown()  <---- DON'T NEED THIS
        # button.bind(on_release=self.dropdown.open)  <---- DON'T NEED THIS
        self.dropdown.open(button)  # you need this to open the dropdown
        # print(button.text)
  

class MainWindow(Screen):
    pass

class SecondWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass


kv = Builder.load_file("proj.kv")

class MyMainApp(App):
    def build(self):
        return kv

    

if __name__ == "__main__":
    MyMainApp().run()

Kv file

 WindowManager:
    MainWindow:
    SecondWindow:
    ModelWindow:

<ModelWindow>:
    name: "model"
 
<MainWindow>:
    name: "main"
    

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height

        Button:
            text: "Select Model"
            on_press:
                app.root.current = "model" 
                      
        Button:
            text: "Test Model"

        Button:
            text: "Create New Model"
            on_release: 
                app.root.current = "second" 
                root.manager.transition.direction = "left"


<SecondWindow>:
    name: "second"
    Button:
        text: "Go Back"
        on_release:
            app.root.current = "main"
            root.manager.transition.direction = "right"
          

My I ask if anyone knows how to make a dropdown list in the next window? should I write it in the kv file or the python itself? Thank youu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜司空 2025-01-17 23:01:37

如果我正确理解您的观点,您想要初始化 ModelWindow 类。为此,您可以简单地调用 __init__ 中的方法 window 并进行一些修改,或者直接在 __init__ 中将所有内容定义为,

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        box = BoxLayout(orientation='vertical')
        label = Label(text='LABEL')
        ...
        # Same as in your method `window`.
            ...
            self.dropdown.add_widget(btn)
        self.add_widget(box)

If I get your point correctly, you want to initialize the ModelWindow class. In order to do that you can simply call the method window in __init__ with some modifications or define everything directly in __init__ as,

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        box = BoxLayout(orientation='vertical')
        label = Label(text='LABEL')
        ...
        # Same as in your method `window`.
            ...
            self.dropdown.add_widget(btn)
        self.add_widget(box)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文