python - 如何关闭另一个班级的kivy pop?
问题描述
蟒蛇文件:
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
class MainPage(FloatLayout):
con = ObjectProperty(None)
def __init__(self,**kwargs):
super(MainPage,self).__init__(**kwargs)
pass
def openme(self):
Pops().open()
def openpop(self):
con = Pops2()
con.content = Content()
con.open()
class Pops(Popup):
def __init__(self,**kwargs):
super(Pops,self).__init__(**kwargs)
pass
def closeme(self):
self.dismiss()
class Pops2(Popup):
def __init__(self,**kwargs):
super(Pops2,self).__init__(**kwargs)
pass
def closeme(self):
self.dismiss()
class Content(BoxLayout):
def __init__(self,**kwargs):
super(Content,self).__init__(**kwargs)
pass
def closepops2(self):
Pops2().dismiss()
class PopCheck(App):
def build(self):
self.root = Builder.load_file('PopCheck.kv')
return MainPage()
if __name__ == '__main__':
PopCheck().run()
.kv 文件:
#:import Factory kivy.factory.Factory
<MainPage>:
Button:
text: 'Open pop1'
pos_hint:{'x': .1,'top': .2}
size_hint:(.1,.1)
on_press: root.openme()
Button:
text: 'Open pop2'
pos_hint:{'x': .75,'top': .2}
size_hint:(.1,.1)
on_press: root.openpop()
<Pops>:
title: 'Content within the popup is displaying twice under kivy1.10.1 with python3.x'
id: pop
size_hint: None, None
size: 400, 220
auto_dismiss: True
#height: container.height
separator_color: 255,0,0,0.9
BoxLayout:
orientation: 'vertical'
pos: self.pos
size: root.size
id: container
GridLayout:
orientation: 'horizontal'
cols: 1
TextInput:
id: usr
hint_text: 'User Name'
multiline: False
write_tab:False
text: 'sri'
markup: True
on_text: root.focus_on()
TextInput:
id: psd
multiline: False
write_tab:False
hint_text:'Password'
password: True
text: 'sri'
#input_filter: 'int'
on_text: root.focus_on()
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: 45
Button:
text: 'Cancel'
background_color: 255,0,0,0.9
on_press: root.closeme()
Button:
text: 'Login'
background_color: 0,1,255,0.7
Label:
id: er
foreground_color: 1, 250, 100, 1
color: 1, 0.67, 0, 1
size_hint_y: None
height: 0
text:''
font_size: '12pt'
<Pops2>:
title: 'Content from seperate Class but no close option'
size_hint: .75,.50
auto_dismiss: True
#height: container.height
separator_color: 255,0,0,0.9
<Content>:
orientation: 'vertical'
pos: self.pos
size: root.size
TextInput:
id: vehno
hint_text:'Name'
multiline: False
write_tab: False
TextInput:
id: stopnam
hint_text: 'Roll No'
allow_copy: False
password: True
multiline: False
write_tab: False
TextInput:
id: descr
hint_text: 'Department'
allow_copy: False
password: True
multiline: False
write_tab: False
TextInput:
id: descr
hint_text: 'Year'
allow_copy: False
password: True
multiline: False
write_tab: False
Button:
text: 'Close'
on_press: root.closepops2()#Factory.Pops2().dismiss()
解决方案
解决方案
closeme()
通过 inovking方法关闭第二个 Popup class Pops2()
。详情请参阅示例。
.kv 文件
- 删除工厂的导入声明
- 替换
root.closepops2()
为app.root.con.closeme()
蟒蛇文件
- 替换
con
为self.con
in 方法openpop()
super(...)
如果在构造函数旁边和构造函数中没有其他代码,pass
则删除所有构造函数。
例子
主文件
import kivy
kivy.require('1.11.0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
class MainPage(FloatLayout):
con = ObjectProperty(None)
def openme(self):
Pops().open()
def openpop(self):
self.con = Pops2()
self.con.content = Content()
self.con.open()
class Pops(Popup):
def closeme(self):
self.dismiss()
class Pops2(Popup):
def closeme(self):
self.dismiss()
class Content(BoxLayout):
pass
class PopCheck(App):
def build(self):
self.root = Builder.load_file('PopCheck.kv')
return MainPage()
if __name__ == '__main__':
PopCheck().run()
流行检查.kv
#:kivy 1.11.0
<MainPage>:
Button:
text: 'Open pop1'
pos_hint:{'x': .1,'top': .2}
size_hint:(.1,.1)
on_press: root.openme()
Button:
text: 'Open pop2'
pos_hint:{'x': .75,'top': .2}
size_hint:(.1,.1)
on_press: root.openpop()
<Pops>:
title: 'Content within the popup is displaying twice under kivy1.10.1 with python3.x'
id: pop
size_hint: None, None
size: 400, 220
auto_dismiss: True
#height: container.height
separator_color: 255,0,0,0.9
BoxLayout:
orientation: 'vertical'
pos: self.pos
size: root.size
id: container
GridLayout:
orientation: 'horizontal'
cols: 1
TextInput:
id: usr
hint_text: 'User Name'
multiline: False
write_tab:False
text: 'sri'
markup: True
on_text: root.focus_on()
TextInput:
id: psd
multiline: False
write_tab:False
hint_text:'Password'
password: True
text: 'sri'
#input_filter: 'int'
on_text: root.focus_on()
BoxLayout:
orientation: 'horizontal'
size_hint_y: None
height: 45
Button:
text: 'Cancel'
background_color: 255,0,0,0.9
on_press: root.closeme()
Button:
text: 'Login'
background_color: 0,1,255,0.7
Label:
id: er
foreground_color: 1, 250, 100, 1
color: 1, 0.67, 0, 1
size_hint_y: None
height: 0
text:''
font_size: '12pt'
<Pops2>:
title: 'Content from seperate Class but no close option'
size_hint: .75,.50
auto_dismiss: True
#height: container.height
separator_color: 255,0,0,0.9
<Content>:
orientation: 'vertical'
pos: self.pos
size: root.size
TextInput:
id: vehno
hint_text:'Name'
multiline: False
write_tab: False
TextInput:
id: stopnam
hint_text: 'Roll No'
allow_copy: False
password: True
multiline: False
write_tab: False
TextInput:
id: descr
hint_text: 'Department'
allow_copy: False
password: True
multiline: False
write_tab: False
TextInput:
id: descr
hint_text: 'Year'
allow_copy: False
password: True
multiline: False
write_tab: False
Button:
text: 'Close'
on_press:
app.root.con.closeme()
输出
推荐阅读
- javascript - 获取和设置内容可编辑 div 中的光标位置
- c++ - 如何使用 gnu gcc 标志 -mpc32、-mpc64 和 -mpc80?
- logging - 我正在从 cmd 连接到我的服务器并运行一些脚本。我需要将这些脚本的日志保存到我的本地桌面文件夹中
- database - 将 UserState 存储在外部数据库中的最佳密钥
- python - 使用 Beautiful Soup 从选定标签中提取文本
- ios - AVAudioEngineImpl::IOUnitConfigurationChanged() 崩溃
- java - 使用 org.json 库格式化 LocalDateTime 的问题
- postfix-mta - DKIM 仍被声明为无
- cypress - 赛普拉斯测试 - 期望输入为空
- node.js - 为什么反应创建应用程序中缺少 src 和公用文件夹?