gpt4 book ai didi

python - 这是 Kivy 的一个错误吗? DropDown + ScreenManager 未按预期工作

转载 作者:行者123 更新时间:2023-12-01 04:18:22 24 4
gpt4 key购买 nike

如果在带有 DropDown 的屏幕之前有屏幕,我在使用 ScreenManager 和 DropDown 时会遇到可重现的困难。我已经为此苦苦挣扎了好几天,因为我是初学者,我认为这是我的错。
我将代码融化到了问题的核心,因此我的预期功能丢失了。目的是增量搜索字段,以便只有适当的选项才会显示在下拉列表中。这就是为什么我需要一个绑定(bind)到下拉按钮的输入小部件。我有一个解决方案,只是屏幕管理器无法按预期工作。
请考虑这段代码:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.properties import ListProperty, StringProperty
import re
from kivy.lang import Builder

Builder.load_string('''
<Intro>:
BoxLayout:
Button:
text: 'Press to go to SecondScreen'
font_size: '20px'
on_release: root.manager.current = 'SecondScreen'
<SecondScreen>:
ComboLayout:
Label:
text: "Dropdown on SecondScreen \\n\\n if ComboEdit.text doesn't have \\n a non-empty string assigned \\n code is broken!"
font_size: '20px'
ComboEdit:
size_hint: .5, .5
pos_hint: {'center':(.5, .5)}
on_text: self.parent.on_text(self, args[1])
text: 'X' ## <<<=== THIS IS NECESSARY, REALLY!?!
font_size: '100px'
multiline: False
''')

class ComboEdit(TextInput):
options = ListProperty(('',))

def __init__(self, **kw):
ddn = self.drop_down = DropDown()
ddn.bind(on_select=self.on_select)
super(ComboEdit, self).__init__(**kw)

def on_options(self, instance, value):
ddn = self.drop_down
ddn.clear_widgets()
for option in value:
but = Button(text=option,
size_hint_y=None,
height='36sp',
on_release=lambda btn: ddn.select(btn.text))
ddn.add_widget(but)

def on_select(self, instance, value):
self.text = value

class ComboLayout(BoxLayout):
def on_text(self, instance, value):
instance.options = [str(i) for i in range(0,8)]
instance.drop_down.open(instance)

class Intro(Screen):
pass
class SecondScreen(Screen):
pass

class BugDemoApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(Intro(name='Intro'))
sm.add_widget(SecondScreen(name='SecondScreen'))
return sm

if __name__ == '__main__':
BugDemoApp().run()

偶然我发现:我需要为 ComboEdit.text 分配一些非空字符串,如果没有 - 将其作为注释或分配“” - 我得到此回溯:

   File "C:/Users/ORANG/PycharmProjects/waldi/playground/widgets.py", line 56, in on_text
instance.drop_down.open(instance)
File "C:\Kivy-1.9.0-py2.7-win32-x64\kivy27\kivy\uix\dropdown.py", line 215, in open
'Cannot open a dropdown list on a hidden widget')
kivy.uix.dropdown.DropDownException: Cannot open a dropdown list on a hidden widget

我的问题是:

1)这可能是一个错误还是我在这里做错了什么?

2)如果它是一个错误,那么在哪里发布它的正确位置?

3)目前对我来说最重要的是:现在用户必须手动从编辑字段中删除 ComboEdit.text 中给定的字符串,以用自己的输入替换它。这太丑了。您有解决方法的想法吗?是否可以选择虚拟文本,以便如果用户开始输入,它将立即被覆盖?

我尝试这样做

   ComboEdit:
...
focus: True
select_all: True

但是没有效果?

任何对该错误的承认、任何解决该错误的提示、任何解释我在这里做错的事情都将非常非常欢迎!

最佳答案

可以通过很多(更好?)的方式来完成,但这可行。

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.properties import ListProperty, StringProperty
import re
from kivy.lang import Builder

Builder.load_string('''
<Intro>:
BoxLayout:
Button:
text: 'Press to go to SecondScreen'
font_size: '20px'
on_release: root.manager.current = 'SecondScreen'
<SecondScreen>:
ComboLayout:
Label:
text: "working?"
font_size: '20px'
ComboEdit:
size_hint: .5, .5
pos_hint: {'center':(.5, .5)}
font_size: '100px'
multiline: False
''')

class ComboEdit(TextInput):
options = ListProperty([])

def __init__(self, **kw):
super(ComboEdit, self).__init__(**kw)
self.ddn = DropDown()
self.ddn.bind(on_select=self.on_select)

def on_options(self, instance, value):

for option in value:
but = Button(text=option,
size_hint_y=None,
height='36sp',
on_release=lambda btn: self.ddn.select(btn.text))
self.ddn.add_widget(but)

def on_select(self, instance, value):
self.text = value

def on_text(self, instance, value):

self.options = [str(i) for i in range(0,8)]
if not self.get_root_window():
return # do proceed if I'm not displayed <=> If have no parent
self.ddn.open(self)


class ComboLayout(BoxLayout):
pass

class Intro(Screen):
pass

class SecondScreen(Screen):
pass

class BugDemoApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(Intro(name='Intro'))
sm.add_widget(SecondScreen(name='SecondScreen'))
return sm

if __name__ == '__main__':
BugDemoApp().run()

希望有帮助。

关于python - 这是 Kivy 的一个错误吗? DropDown + ScreenManager 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34044266/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com