python文件:
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()
答案 0 :(得分:1)
通过调用closeme()
中的class Pops2()
方法来关闭第二个Popup。有关详细信息,请参阅示例。
root.closepops2()
替换为app.root.con.closeme()
con
中用self.con
替换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()
#: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()