Python Kivy:discouse()不';我无法使用我的ModalView小部件
我正在制作一个Python Kivy:discouse()不';我无法使用我的ModalView小部件,python,kivy,kivy-language,Python,Kivy,Kivy Language,我正在制作一个ModalView小部件,并试图通过按下按钮来关闭它。按钮按下的回调转到假定关闭它的方法 这是主电压,0.kv: <MainFrame>: id: main_frame ScreenMaster: id: screen_master StartScreen: id: start_screen SettingsPopup: id: setting
ModalView
小部件,并试图通过按下按钮来关闭它。按钮按下的回调转到假定关闭它的方法
这是主电压,0.kv:
<MainFrame>:
id: main_frame
ScreenMaster:
id: screen_master
StartScreen:
id: start_screen
SettingsPopup:
id: settings_popup
GameScreen:
id: game_screen
GameOverPopup:
id: gameover_popup
再次按
Play
按钮调用done()
方法,但ModalView
小部件不会关闭。如何解决这个问题并使其消失?我自己已经找到了解决方案:
<MainFrame>:
id: main_frame
ScreenMaster:
id: screen_master
StartScreen:
id: start_screen
SettingsPopup:
id: settings_popup
on_parent: if self.parent == start_screen: start_screen.remove_widget(self)
GameScreen:
id: game_screen
GameOverPopup:
id: gameover_popup
on_parent: if self.parent == game_screen: game_screen.remove_widget(self)
:
id:主框架
编剧:
id:屏幕/主屏幕
StartScreen:
id:启动屏幕
设置SPOPUP:
id:设置\u弹出窗口
在父屏幕上:如果self.parent==开始屏幕:开始屏幕。删除小部件(self)
游戏屏幕:
id:游戏屏幕
游戏人口:
id:gameover_弹出窗口
在父屏幕上:如果self.parent==游戏屏幕:游戏屏幕。删除小部件(self)
因此,我们实例化这些弹出窗口并立即关闭它们。现在,我们可以随时打开备份。我自己也找到了解决方案:
<MainFrame>:
id: main_frame
ScreenMaster:
id: screen_master
StartScreen:
id: start_screen
SettingsPopup:
id: settings_popup
on_parent: if self.parent == start_screen: start_screen.remove_widget(self)
GameScreen:
id: game_screen
GameOverPopup:
id: gameover_popup
on_parent: if self.parent == game_screen: game_screen.remove_widget(self)
:
id:主框架
编剧:
id:屏幕/主屏幕
StartScreen:
id:启动屏幕
设置SPOPUP:
id:设置\u弹出窗口
在父屏幕上:如果self.parent==开始屏幕:开始屏幕。删除小部件(self)
游戏屏幕:
id:游戏屏幕
游戏人口:
id:gameover_弹出窗口
在父屏幕上:如果self.parent==游戏屏幕:游戏屏幕。删除小部件(self)
因此,我们实例化这些弹出窗口并立即关闭它们。现在,我们可以随时打开它们。
ModalView
或从中继承的任何东西(弹出窗口
)都不能作为子对象添加到任何位置。默认情况下,ModalView
直接附加到窗口
,如调用其open()
方法时所述
当您将其作为子部件添加到某个位置时,您会自动使其可见,这是不好的,因为您滥用了该小部件的用途
相反,您应该将其作为某个类/规则,并在需要显示时调用open()
方法。当您需要手动关闭它时,您需要将一些方法附加到打开的上,以将ModalView
类的实例存储在某个位置,并通过该实例调用disclose()
,或者在ModalView
上的某个位置放置一个按钮
根据您的回答,您只需修补误用小部件的错误。:)
有两种方法可以打开ModalView
(或弹出窗口
):
python:
SettingsPopup().open()
#:import Factory kivy.factory.Factory
Factory.SettingsPopup().open()
kv:
SettingsPopup().open()
#:import Factory kivy.factory.Factory
Factory.SettingsPopup().open()
ModalView
或从中继承的任何内容(Popup
)都不能作为子项添加到任何位置。默认情况下,ModalView
直接附加到窗口
,如调用其open()
方法时所述
当您将其作为子部件添加到某个位置时,您会自动使其可见,这是不好的,因为您滥用了该小部件的用途
相反,您应该将其作为某个类/规则,并在需要显示时调用open()
方法。当您需要手动关闭它时,您需要将一些方法附加到打开的上,以将ModalView
类的实例存储在某个位置,并通过该实例调用disclose()
,或者在ModalView
上的某个位置放置一个按钮
根据您的回答,您只需修补误用小部件的错误。:)
有两种方法可以打开ModalView
(或弹出窗口
):
python:
SettingsPopup().open()
#:import Factory kivy.factory.Factory
Factory.SettingsPopup().open()
kv:
SettingsPopup().open()
#:import Factory kivy.factory.Factory
Factory.SettingsPopup().open()
是的,你说得对。它导致“弱引用对象不再存在”错误。但问题是,我需要这份证明。我想说的是:在那些设置spopup和gameoverpoup上,我有一个按钮,它们有对各自类的回调,但是没有这个引用,我无法通过ID访问GameScreen或StartScreen的任何方法。例如,在GameOverPopup上,我有一个按钮“再次播放”,它调用gamescreen.game\u restart(),如果我没有这个引用,我就不能调用它…@Roman然后将它存储在某个地方,而不是将它作为子项添加到某个地方:)@Roman你可以使用弹出窗口的方法来执行此操作,例如:App.get\u running\u App().my_popup=self
然后在kv do应用程序中.my_popup
。另外,在“关闭”事件的中执行App.get_running_App()。my_popup=None
。)实际上我找到了另一种方法:)从这里开始:引用Widgets部分,在引用id中使用self。是的,你是对的。它导致“弱引用对象不再存在”错误。但问题是,我需要这份证明。我想说的是:在那些设置spopup和gameoverpoup上,我有一个按钮,它们有对各自类的回调,但是没有这个引用,我无法通过ID访问GameScreen或StartScreen的任何方法。例如,在GameOverPopup上,我有一个按钮“再次播放”,它调用gamescreen.game\u restart(),如果我没有这个引用,我就不能调用它…@Roman然后将它存储在某个地方,而不是将它作为子项添加到某个地方:)@Roman你可以使用弹出窗口的方法来执行此操作,例如:App.get\u running\u App().my_popup=self
然后在kv do应用程序中.my_popup
。另外,在“关闭”事件的中执行App.get_running_App()。my_popup=None
。)实际上我找到了另一种方法:)从这里开始:引用Widgets部分,在引用id中使用self。