关闭 Kivy 中的弹出窗口 Window
Dismiss Pop Up Window in Kivy
我正在尝试关闭 KV 语言的弹出窗口 window。我曾尝试创建一个函数,但在调用弹出窗口 window 的实例时遇到问题,因为它不是它自己的 class 对象。我也尝试过使用工厂模块,但这也不起作用,因为出于某种原因,这只会让我在弹出 window 中使用一个小部件。下面是我的代码:
Python 文件:
class SettingsWindow(Screen):
pretty_list_people = StringProperty ("")
pretty_list_jobs = StringProperty ("")
def get_Jobs(self):
return WindowManager.jobs
def get_People(self):
return WindowManager.people
def Pretty_Print_People(self, ppl_list):
self.pretty_list_people = ""
for person in ppl_list:
self.pretty_list_people += person + "\n"
def Pretty_Print_Jobs(self, job_list):
self.pretty_list_jobs = ""
for job in job_list:
self.pretty_list_jobs += job + "\n"
def show_popup(self):
show = PopupAddJob()
popupWindow = Popup(title="Add Job", content=show, size_hint=(None, None), size=(200, 200))
popupWindow.open()
def dismiss_popup(self):
self.popupWindow.dismiss()
kv 文件:
<PopupAddJob>:
Label:
text: "You pressed a button"
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "top":1}
TextInput:
id: add_job
multiline: False
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "y":0.5}
Button:
text: "Close"
on_press:add_job.focus=True
on_press: SettingsWindow.dismiss_popup() # <<<<<<<<<< *** PROBLEM IS HERE ***
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "y":0.3}
在您的 dismiss_popup()
方法中您引用了 self.popupWindow
,但您还没有创建这样的属性。也许您应该在 show_popup()
方法中创建该引用:
def show_popup(self):
show = PopupAddJob()
self.popupWindow = Popup(title="Add Job", content=show, size_hint=(None, None), size=(200, 200))
self.popupWindow.open()
我正在尝试关闭 KV 语言的弹出窗口 window。我曾尝试创建一个函数,但在调用弹出窗口 window 的实例时遇到问题,因为它不是它自己的 class 对象。我也尝试过使用工厂模块,但这也不起作用,因为出于某种原因,这只会让我在弹出 window 中使用一个小部件。下面是我的代码:
Python 文件:
class SettingsWindow(Screen):
pretty_list_people = StringProperty ("")
pretty_list_jobs = StringProperty ("")
def get_Jobs(self):
return WindowManager.jobs
def get_People(self):
return WindowManager.people
def Pretty_Print_People(self, ppl_list):
self.pretty_list_people = ""
for person in ppl_list:
self.pretty_list_people += person + "\n"
def Pretty_Print_Jobs(self, job_list):
self.pretty_list_jobs = ""
for job in job_list:
self.pretty_list_jobs += job + "\n"
def show_popup(self):
show = PopupAddJob()
popupWindow = Popup(title="Add Job", content=show, size_hint=(None, None), size=(200, 200))
popupWindow.open()
def dismiss_popup(self):
self.popupWindow.dismiss()
kv 文件:
<PopupAddJob>:
Label:
text: "You pressed a button"
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "top":1}
TextInput:
id: add_job
multiline: False
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "y":0.5}
Button:
text: "Close"
on_press:add_job.focus=True
on_press: SettingsWindow.dismiss_popup() # <<<<<<<<<< *** PROBLEM IS HERE ***
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "y":0.3}
在您的 dismiss_popup()
方法中您引用了 self.popupWindow
,但您还没有创建这样的属性。也许您应该在 show_popup()
方法中创建该引用:
def show_popup(self):
show = PopupAddJob()
self.popupWindow = Popup(title="Add Job", content=show, size_hint=(None, None), size=(200, 200))
self.popupWindow.open()