我已经创建了一个DropDown列表,但是我只想打开一个小箭头。当我单击箭头时,列表将打开,并且箭头必须大于箭头按钮。因此,我将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
在此示例代码中,我只放置了一个小盒子,但问题是相同的。
答案 0 :(得分:1)
您可以在MainWindow
类中添加一个方法来执行您想要的操作。无需调用DropDownList
版本上的open()
Button
方法,只需调用new方法即可。因此,添加如下方法:
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
方法。这是在用私有方法搞乱,因此不建议使用。对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)