我创建了一个下拉列表,但我只想要一个小箭头来打开它。当我单击箭头时,列表将打开,并且项目必须大于箭头按钮。所以我将 auto_width 设置为 false。现在的问题是列表在箭头按钮的左侧对齐。我希望它位于中心,就像 halign = 'center' 选项一样。但它不起作用,而且我找不到能起作用的。
这是 python 文件:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
# from kivy.clock import Clock
# from crialista import running
# from time import sleep
class MainWindow(Screen):
min = ObjectProperty(None)
seg = ObjectProperty(None)
def __init__(self, **kw):
super().__init__(**kw)
self.min_down = DropDown()
self.sec_down = DropDown()
for x in range(61):
btn_min = Button(text=str(x), font_size=30, size_hint_y=None, height=50) btn_min.bind(on_release=lambda btn: self.minute(btn.text))
btn_min.bind(on_release=lambda dismiss: self.min_down.dismiss())
self.min_down.add_widget(btn_min)
self.min_down.auto_width = False
def minute(self, texto):
self.min.text = texto
class NumericInput(TextInput):
def insert_text(self, string, from_undo=False):
new_text = self.text + string
self.input_filter = 'int'
if new_text != "":
try:
if int(new_text) >= 0:
self.text = new_text
if int(new_text) > 60:
self.text = "60"
if len(new_text) > 2:
self.text = new_text[:-1]
except ValueError:
TextInput.insert_text(self, "", from_undo=from_undo)
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("teste.kv")
class TesteApp(App):
def build(self):
return kv
if __name__ == "__main__":
TesteApp().run()
和 kv 文件:
WindowManager:
MainWindow:
<MainWindow>:
name: "main"
min: min
FloatLayout:
Button:
pos_hint:{'center_x': .27, 'center_y': .6}
size_hint: .03, .05
on_release: root.min_down.open(self)
NumericInput:
id: min
pos_hint:{'center_x': .4, 'center_y': .6}
size_hint: .15, .1
input_filter: "int"
hint_text: '00'
hint_text_color: (0, 0, 0, 1)
font_size: (root.width/25 + root.height/25)
halign: "center"
multiline: False
在这个示例代码中,我只放了一个小盒子,但问题是一样的。
最佳答案
您可以在 MainWindow
类中添加一个方法来执行您想要的操作。无需在 Button
版本上调用 DropDownList
open()
方法,只需调用新方法即可。因此添加一个方法,例如:
def position_and_open_min_down(self, button):
# open the DropDownList
self.min_down.open(button)
# now adjust its position
self.min_down.x -= self.min_down.width/2
并在kv
中,将Button
规则更改为:
Button:
pos_hint:{'center_x': .27, 'center_y': .6}
size_hint: .03, .05
on_release: root.position_and_open_min_down(self)
如上所述,这可行,但随后 DropDown
会滑回来。我能想到的解决这个问题的唯一方法是重写实际计算 DropDown 位置的 DropDown 方法。这会扰乱私有(private)方法,因此强烈建议不要这样做。对 DropDown 类的任何更改都可能会造成困惑。我建议您忍受在不进行此修改的情况下使用 DropDown
带来的轻微不便。做出免责声明后,以下是一个可能的解决方案:
创建一个新类(MyDropDown
):
class MyDropDown(DropDown):
def _reposition(self, *largs):
super(MyDropDown, self)._reposition(*largs)
widget = self.attach_to
if not widget:
return
wx, wy = widget.to_window(*widget.pos)
# do centering calculation here
x = wx - self.width/2
# everything else is just to keep the DropDown inside the window
if x < 0:
x = 0
win = self._win
if not win:
self.x = x
return
if x + self.width > win.width:
x = win.width - self.width
self.x = x
_reposition()
方法用于计算DropDown
的位置,上面只是重新计算x
。
将对 DropDown
的调用替换为 MyDropDown
,并删除
self.min_down.x -= self.min_down.width/2
您还可以将 Button
操作恢复为:
on_release: root.min_down.open(self)
关于python - 如何将 kivy 中的下拉列表与主按钮中心对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57702232/