首页 > 解决方案 > 如何关闭另一个班级的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()

标签: pythonpython-2.7kivy

解决方案


解决方案

closeme()通过 inovking方法关闭第二个 Popup class Pops2()。详情请参阅示例。

.kv 文件

  1. 删除工厂的导入声明
  2. 替换root.closepops2()app.root.con.closeme()

蟒蛇文件

  1. 替换conself.conin 方法openpop()
  2. 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()

输出

Img01 - 弹出窗口 2 Img02 - 弹出窗口 2 已关闭


推荐阅读