Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Kivy中,如何使用另一个类中的关闭按钮关闭弹出窗口?_Python_Popup_Kivy_Kivy Language - Fatal编程技术网

Python 在Kivy中,如何使用另一个类中的关闭按钮关闭弹出窗口?

Python 在Kivy中,如何使用另一个类中的关闭按钮关闭弹出窗口?,python,popup,kivy,kivy-language,Python,Popup,Kivy,Kivy Language,我需要用一些关闭按钮关闭kivy中的弹出窗口 我在这里找到了一些解决方案,但它与我目前使用的ScreenManager不兼容。要像我想要的那样显示我的弹出窗口,我使用FloatLayout并将其作为弹出窗口的内容传递。 当我使用close函数时,它在defloatlayout类内部调用,但不起作用。那么如何关闭主窗口中的弹出窗口 这是我的密码: from kivy.app import App from kivy.uix.screenmanager import ScreenMa

我需要用一些关闭按钮关闭kivy中的弹出窗口

我在这里找到了一些解决方案,但它与我目前使用的ScreenManager不兼容。要像我想要的那样显示我的弹出窗口,我使用FloatLayout并将其作为弹出窗口的内容传递。 当我使用close函数时,它在defloatlayout类内部调用,但不起作用。那么如何关闭主窗口中的弹出窗口

这是我的密码:

    from kivy.app import App
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivy.lang import Builder
    from kivy.uix.popup import Popup
    from kivy.uix.floatlayout import FloatLayout

    class MainWindow(Screen):
        def open(self):
            pop = Pop()
            popup = Popup(title="",
                          content=pop,
                          size_hint=(.8, .8))
            popup.open()

    class Pop(FloatLayout):
        def close(self):
            self.dismiss()

    class Setting(Screen):
        pass

    class WindowManager(ScreenManager):
        pass

    kv = Builder.load_file("teste.kv")

    class TesteApp(App):
        def build(self):
            return kv

    if __name__ == "__main__":
        TesteApp().run()
对于我的kv文件:

    #:import NoTransition kivy.uix.screenmanager.NoTransition
    #:import SlideTransition kivy.uix.screenmanager.SlideTransition

    WindowManager:
        MainWindow:
        Setting:


    <MainWindow>:
        name: "main"

        FloatLayout:
            Label:
                pos_hint:{'center_x': .5, 'center_y': .8}
                size_hint:0.5, 0.5
                text: "TITLE"
                font_size: (root.width/30 + root.height/30)

            Button:
                pos_hint:{'center_x': .5, 'center_y': .4}
                size_hint:0.6, 0.1
                text: "Set"
                on_release:
                    app.root.transition = SlideTransition(direction='left')
                    app.root.current = "setting"

            Button:
                pos_hint:{'center_x': .5, 'center_y': .25}
                size_hint:0.6,0.1
                text: "Pop"
                on_release:
                    root.open()


    <Setting>:
        name: "setting"

        FloatLayout:
            Label:
                text: 'Set Time'
                pos_hint:{'center_x': .5, 'center_y': .75}
                size_hint: 0.1, 0.1
                font_size: (root.width/30 + root.height/30)

            Button:
                pos_hint:{'center_x': .1, 'center_y': .1}
                size_hint:0.05,0.05
                on_release:
                    app.root.transition = SlideTransition(direction='right')
                    app.root.current = 'main'

    <Pop>:
        Label:
            text: 'Popup text'
            size_hint: .4, .15
            pos_hint:{'center_x': .5, 'center_y': .7}
            halign: "center"
            valign: "center"

        Button:
            text: "Close"
            size_hint: .4, .15
            pos_hint:{'center_x': .5, 'center_y': .15}
            on_release: root.close()
#:导入nottransition kivy.uix.screenmanager.nottransition
#:导入SlideTransition kivy.uix.screenmanager.SlideTransition
WindowManager:
主窗口:
设置:
:
名称:“主要”
浮动布局:
标签:
位置提示:{'center_x':.5'center_y':.8}
尺寸提示:0.5,0.5
正文:“标题”
字体大小:(root.width/30+root.height/30)
按钮:
位置提示:{'center_x':.5'center_y':.4}
尺寸提示:0.6,0.1
文本:“设置”
发布时:
app.root.transition=SlideTransition(direction='left')
app.root.current=“设置”
按钮:
位置提示:{'center_x':.5'center_y':.25}
尺寸提示:0.6,0.1
文字:“流行”
发布时:
root.open()
:
名称:“设置”
浮动布局:
标签:
文本:“设置时间”
位置提示:{'center_x':.5'center_y':.75}
大小提示:0.1,0.1
字体大小:(root.width/30+root.height/30)
按钮:
位置提示:{'center_x':.1'center_y':.1}
尺寸提示:0.05,0.05
发布时:
app.root.transition=SlideTransition(direction='right')
app.root.current='main'
:
标签:
文本:“弹出文本”
大小提示:.4,.15
位置提示:{'center_x':.5'center_y':.7}
哈利恩:“中心”
valign:“中心”
按钮:
正文:“结束”
大小提示:.4,.15
位置提示:{'center_x':.5'center_y':.15}
发布时:root.close()

您可以通过在
open()
方法中保存对
Popup
的引用,并将
close()
方法放在同一个类中来完成此操作。在下面修改的代码中,
Close
按钮现在调用
main窗口的
Close()
方法:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout


class MainWindow(Screen):
    def open(self):
        pop = Pop()
        self.popup = Popup(title="",
                      content=pop,
                      size_hint=(.8, .8))
        self.popup.open()

    def close(self):
        self.popup.dismiss()


class Pop(FloatLayout):
    pass


class Setting(Screen):
    pass


class WindowManager(ScreenManager):
    pass


kv = Builder.load_string('''
#:import NoTransition kivy.uix.screenmanager.NoTransition
#:import SlideTransition kivy.uix.screenmanager.SlideTransition

WindowManager:
    MainWindow:
    Setting:


<MainWindow>:
    name: "main"

    FloatLayout:
        Label:
            pos_hint:{'center_x': .5, 'center_y': .8}
            size_hint:0.5, 0.5
            text: "TITLE"
            font_size: (root.width/30 + root.height/30)

        Button:
            pos_hint:{'center_x': .5, 'center_y': .4}
            size_hint:0.6, 0.1
            text: "Set"
            on_release:
                app.root.transition = SlideTransition(direction='left')
                app.root.current = "setting"

        Button:
            pos_hint:{'center_x': .5, 'center_y': .25}
            size_hint:0.6,0.1
            text: "Pop"
            on_release:
                root.open()


<Setting>:
    name: "setting"

    FloatLayout:
        Label:
            text: 'Set Time'
            pos_hint:{'center_x': .5, 'center_y': .75}
            size_hint: 0.1, 0.1
            font_size: (root.width/30 + root.height/30)

        Button:
            pos_hint:{'center_x': .1, 'center_y': .1}
            size_hint:0.05,0.05
            on_release:
                app.root.transition = SlideTransition(direction='right')
                app.root.current = 'main'

<Pop>:
    Label:
        text: 'Popup text'
        size_hint: .4, .15
        pos_hint:{'center_x': .5, 'center_y': .7}
        halign: "center"
        valign: "center"

    Button:
        text: "Close"
        size_hint: .4, .15
        pos_hint:{'center_x': .5, 'center_y': .15}
        on_release: app.root.get_screen('main').close()
        # if you are sure the current screen will still be "main":
        # on_release: app.root.current_screen.close()
''')


class TesteApp(App):
    def build(self):
        return kv


if __name__ == "__main__":
    TesteApp().run()
从kivy.app导入应用
从kivy.uix.screenmanager导入screenmanager,屏幕
从kivy.lang导入生成器
从kivy.uix.popup导入弹出窗口
从kivy.uix.floatlayout导入floatlayout
类主窗口(屏幕):
def打开(自):
pop=pop()
self.popup=弹出窗口(title=“”,
内容=流行音乐,
大小提示=(.8,.8))
self.popup.open()
def关闭(自我):
self.popup.disclose()文件
类Pop(浮动布局):
通过
课程设置(屏幕):
通过
类WindowManager(屏幕管理器):
通过
kv=建筑商荷载字符串(“”)
#:导入NotTransition kivy.uix.screenmanager.NotTransition
#:导入SlideTransition kivy.uix.screenmanager.SlideTransition
WindowManager:
主窗口:
设置:
:
名称:“主要”
浮动布局:
标签:
位置提示:{'center_x':.5'center_y':.8}
尺寸提示:0.5,0.5
正文:“标题”
字体大小:(root.width/30+root.height/30)
按钮:
位置提示:{'center_x':.5'center_y':.4}
尺寸提示:0.6,0.1
文本:“设置”
发布时:
app.root.transition=SlideTransition(direction='left')
app.root.current=“设置”
按钮:
位置提示:{'center_x':.5'center_y':.25}
尺寸提示:0.6,0.1
文字:“流行”
发布时:
root.open()
:
名称:“设置”
浮动布局:
标签:
文本:“设置时间”
位置提示:{'center_x':.5'center_y':.75}
大小提示:0.1,0.1
字体大小:(root.width/30+root.height/30)
按钮:
位置提示:{'center_x':.1'center_y':.1}
尺寸提示:0.05,0.05
发布时:
app.root.transition=SlideTransition(direction='right')
app.root.current='main'
:
标签:
文本:“弹出文本”
大小提示:.4,.15
位置提示:{'center_x':.5'center_y':.7}
哈利恩:“中心”
valign:“中心”
按钮:
正文:“结束”
大小提示:.4,.15
位置提示:{'center_x':.5'center_y':.15}
发布时:app.root.get\u screen('main').close()
#如果您确定当前屏幕仍为“主屏幕”:
#发布时:app.root.current\u screen.close()
''')
类测试应用程序(应用程序):
def生成(自):
回流千伏
如果名称=“\uuuuu main\uuuuuuuu”:
TesteApp().run()