所以我對 Kivy 很陌生,到目前為止非常令人沮喪......無論如何,我現在正在嘗試制作一個可以拖動和移動的彈出窗口,但我不明白發生了什么...... ..當我在 onButtonPress 函數中調用 popup.open() 時,彈出窗口可以通過關閉操作關閉,盡管我失去了可拖動功能....當我通過 self.layout.add_widget( 將彈出窗口直接添加到主窗口時彈出窗口),我可以移動彈出窗口,但無法關閉它......我猜 open() 調用正在重新定義可拖動窗口?這是真的?如果不是,發生了什么以及如何解決它?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貢獻1847條經驗 獲得超11個贊
如果您想拖動Popup
,將其添加到App
layout
可能是最好的方法。當然,那么你并不真的需要使用Popup
,只需使用一些Layout
。如果您走這條路,可以通過定義如下綁定Popup
來完成刪除:Button
closeButton.bind(on_press = lambda x: self.layout.remove_widget(popup))
它lambda
只是用來吸收添加的按鈕實例參數on_press
,因此您不會收到有關該remove_widget()
方法的參數太多的錯誤。
該類modalView
( 的基類Popup
)有一個保持居中的方法Popup
。DragBehavior
如果您覆蓋該方法,看起來會起作用。所以,如果您添加:
def _align_center(self, *l): pass
到你的MoveableImage
班級,我想你就能拖下去了。open()
的方法進行一些ModalView
綁定以確保_align_center()
在任何位置或大小更改時調用該方法。由于將 添加Popup
到 aLayout
不會調用該open()
方法,因此永遠不會設置這些綁定。
添加回答
舉報
0/150
提交
取消