所以我對(duì) Kivy 很陌生,到目前為止非常令人沮喪......無論如何,我現(xiàn)在正在嘗試制作一個(gè)可以拖動(dòng)和移動(dòng)的彈出窗口,但我不明白發(fā)生了什么...... ..當(dāng)我在 onButtonPress 函數(shù)中調(diào)用 popup.open() 時(shí),彈出窗口可以通過關(guān)閉操作關(guān)閉,盡管我失去了可拖動(dòng)功能....當(dāng)我通過 self.layout.add_widget( 將彈出窗口直接添加到主窗口時(shí)彈出窗口),我可以移動(dòng)彈出窗口,但無法關(guān)閉它......我猜 open() 調(diào)用正在重新定義可拖動(dòng)窗口?這是真的?如果不是,發(fā)生了什么以及如何解決它?from kivy.app import Appfrom kivy.uix.widget import Widgetfrom kivy.uix.image import Imagefrom kivy.core.window import Windowfrom kivy.uix.behaviors import DragBehaviorfrom kivy.uix.floatlayout import FloatLayout from kivy.uix.popup import Popup class PopupExample(App): # override the build method and return the root widget of this App def build(self): # Define a grid layout for this App self.layout = FloatLayout() # Add a button self.button = Button(text ="Click for pop-up") self.layout.add_widget(self.button) # Attach a callback for the button press event self.button.bind(on_press = self.onButtonPress) return self.layout def onButtonPress(self, button): # print('opening') layout = GridLayout(cols = 1, padding = 10) img = Image(source="temp_plot.png") closeButton = Button(text = "Close the pop-up") layout.add_widget(img) layout.add_widget(closeButton) popup = MoveableImage( title ='Demo Popup', content = layout, size_hint =(None, None), size = (400,400)) popup.open() #self.layout.add_widget(popup) #closeButton.bind(on_press = self.remove_widget(popup))class MoveableImage(DragBehavior,Popup): def __init__(self, **kwargs): super(MoveableImage, self).__init__(**kwargs) self.drag_timeout = 10000000 self.drag_distance = 0 self.drag_rectangle = [self.x, self.y, self.width, self.height] def on_pos(self, *args): self.drag_rectangle = [self.x, self.y, self.width, self.height] def on_size(self, *args): self.drag_rectangle = [self.x, self.y, self.width, self.height]
1 回答

回首憶惘然
TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊
如果您想拖動(dòng)Popup
,將其添加到App
layout
可能是最好的方法。當(dāng)然,那么你并不真的需要使用Popup
,只需使用一些Layout
。如果您走這條路,可以通過定義如下綁定Popup
來完成刪除:Button
closeButton.bind(on_press = lambda x: self.layout.remove_widget(popup))
它lambda
只是用來吸收添加的按鈕實(shí)例參數(shù)on_press
,因此您不會(huì)收到有關(guān)該remove_widget()
方法的參數(shù)太多的錯(cuò)誤。
該類modalView
( 的基類Popup
)有一個(gè)保持居中的方法Popup
。DragBehavior
如果您覆蓋該方法,看起來會(huì)起作用。所以,如果您添加:
def _align_center(self, *l): pass
到你的MoveableImage
班級(jí),我想你就能拖下去了。open()
的方法進(jìn)行一些ModalView
綁定以確保_align_center()
在任何位置或大小更改時(shí)調(diào)用該方法。由于將 添加Popup
到 aLayout
不會(huì)調(diào)用該open()
方法,因此永遠(yuǎn)不會(huì)設(shè)置這些綁定。
添加回答
舉報(bào)
0/150
提交
取消