如何关闭另一堂课的奇异果流行乐?

时间:2018-07-06 06:09:28

标签: python python-2.7 kivy

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()

1 个答案:

答案 0 :(得分:1)

解决方案

通过调用closeme()中的class Pops2()方法来关闭第二个Popup。有关详细信息,请参阅示例。

kv文件

  1. 删除 Factory
  2. import 语句
  3. root.closepops2()替换为app.root.con.closeme()

Python文件

  1. 在方法con中用self.con替换openpop()
  2. 如果构造函数中super(...)pass旁边没有其他代码,则删除所有构造函数。

示例

main.py

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()

PopCheck.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()

输出

Img01 - Popup 2 Img02 - Popup 2 Closed

相关问题