如何在 Kivy 中为下一个窗口创建下拉列表
单击第一个屏幕中的按钮后,如何在下一个窗口中创建下拉列表,我尝试启动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的观点,您想要初始化 ModelWindow 类。为此,您可以简单地调用
__init__
中的方法window
并进行一些修改,或者直接在__init__
中将所有内容定义为,If I get your point correctly, you want to initialize the
ModelWindow
class. In order to do that you can simply call the methodwindow
in__init__
with some modifications or define everything directly in__init__
as,