Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/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 从FileChooser选择文件时如何关闭弹出窗口_Python_Kivy - Fatal编程技术网

Python 从FileChooser选择文件时如何关闭弹出窗口

Python 从FileChooser选择文件时如何关闭弹出窗口,python,kivy,Python,Kivy,当我用FileChooser打开Popup时,我可以选择一个文件,但不能关闭它后面的Popup。当引用其他类时,是否有人知道如何关闭弹出窗口 class MyFileChooser(FileChooserListView): def on_submit(*args): fp=args[1][0] class MainScreen(BoxLayout): def filebtn(self, instance): self.popup = Pop

当我用
FileChooser
打开
Popup
时,我可以选择一个文件,但不能关闭它后面的
Popup
。当引用其他类时,是否有人知道如何关闭
弹出窗口

class MyFileChooser(FileChooserListView):

    def on_submit(*args):
        fp=args[1][0]

class MainScreen(BoxLayout):

    def filebtn(self, instance):
        self.popup = Popup(title='Select File',
                      content=MyFileChooser(),
                      size_hint=(None, None), size=(400, 400))
        self.popup.open()

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.orientation = 'vertical'
        self.btnfile = Button(text='Open File')
        self.btnfile.bind(on_press=self.filebtn)
        self.add_widget(self.btnfile)
我试过了

class MyFileChooser(FileChooserListView):
    def on_submit(*args):
        fp=args[1][0]
        popup.dismiss()

但那不行,所以我迷路了。任何帮助都将不胜感激

好的,我知道了,我将弹出窗口重新定义为一个全局窗口,然后我可以从MyFileChooser类中引用它

def filebtn(self, instance):
        global popup
        popup = Popup(title='Select File',
                      content=MyFileChooser(),
                      size_hint=(None, None), size=(400,400))
        popup.open()
然后在MyFileChooser类中

class MyFileChooser(FileChooserListView):

    def on_submit(*args):
        print(args[1][0])
        global fp
        fp = args[1][0]
        print(fp)
        popup.dismiss()

弹出窗口
似乎可以作为曾祖父访问:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.filechooser import FileChooserListView
from kivy.lang import Builder

Builder.load_string('''
<MyWidget>:
    TabbedPanelItem:
        text: 'tab1'
    TabbedPanelItem:
        text: 'tab2'
''')


class MyFileChooser(FileChooserListView):
    def on_submit(self, *args):
        fp=args[0][0]
        self.parent.parent.parent.dismiss()


class MainScreen(BoxLayout):

    def filebtn(self, instance):
        self.popup = Popup(title='Select File',
                      content=MyFileChooser(),
                      size_hint=(None, None), size=(400, 400))
        self.popup.open()

    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.orientation = 'vertical'
        self.btnfile = Button(text='Open File')
        self.btnfile.bind(on_press=self.filebtn)
        self.add_widget(self.btnfile)


class MyApp(App):
    def build(self):
        return MainScreen()


if __name__ == '__main__':
    MyApp().run()
从kivy.app导入应用
从kivy.uix.boxlayout导入boxlayout
从kivy.uix.popup导入弹出窗口
从kivy.uix.button导入按钮
从kivy.uix.filechooser导入FileChooserListView
从kivy.lang导入生成器
Builder.load_字符串(“”)
:
选项卡式面板项目:
文本:“tab1”
选项卡式面板项目:
文本:“tab2”
''')
类MyFileChooser(FileChooserListView):
提交时的def(自我,*参数):
fp=args[0][0]
self.parent.parent.parent.disclose()文件
类主屏幕(框布局):
def filebtn(自身,实例):
self.popup=popup(title='Select File',
content=MyFileChooser(),
大小提示=(无,无),大小=(400400))
self.popup.open()
定义初始(自我,**kwargs):
超级(主屏幕,自我)。\uuuuu初始化(**kwargs)
self.orientation='vertical'
self.btnfile=按钮(text='Open File')
self.btnfile.bind(按=self.filebtn)
self.add\u小部件(self.btnfile)
类别MyApp(应用程序):
def生成(自):
返回主屏幕()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
MyApp().run()

当然,如果您以与弹出内容不同的方式使用MyFileChooser类,则此代码将被破坏。

通常认为使用globals是一个坏主意。如果您确实需要这样做,通常最好将变量作为
App
实例atribute传递,并且可以从Kivy应用程序的任何部分访问该变量。首先,请参阅以下答案:您可以使用
App
内部
kv
文件和
App.get\u running\u App()
以获取当前运行的
App
类实例。