如何从另一个python文件运行文件 [英] how to run a file from another python file

查看:132
本文介绍了如何从另一个python文件运行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.lang import Builder


class CustDrop(DropDown):
    def __init__(self, **kwargs):
        super(CustDrop, self).__init__( **kwargs)
        self.select('')


kv_str = Builder.load_string('''


BoxLayout:
    orientation: 'vertical'
    BoxLayout:
        canvas.before:
            Rectangle:
                pos: self.pos
                size: self.size
            Color:
                rgb: (1,1,1)
        size_hint_y:1

        Button:
            id: btn
            text: 'test'
            on_release: dropdown.open(self)
            #size_hint_y: None
            #height: '48dp'  


        CustDrop:

            id: dropdown

            Button:
                text: 'Run another script'
                size_hint_y: None
                height: '48dp'

        Label:
            size_hint_x: 4

    Label:
        size_hint_y: 9

''')


class ExampleApp(App):
    def build(self):
        return kv_str

if __name__ =='__main__':
    ExampleApp().run()

b.py

import kivy
kivy.require('1.0.6') # replace with your current kivy version !

from kivy.app import App
from kivy.uix.label import Label


class MyApp(App):

    def build(self):
        return Label(text='Hello world')


if __name__ == '__main__':
    MyApp().run()

当我在此之后运行a.py文件时,我单击测试,然后下拉菜单显示``运行另一个脚本''.当我单击``运行另一个脚本''(测试的子菜单)时,然后如何运行b.py(MyApp( ).run()).应在新窗口中打印"Hello world".

when i run a.py file after that i click on test then dropdown show 'run another script'.when i click on 'run another script'(sub menu of test) then how to run b.py(MyApp().run()).It should be print 'Hello world' in new window.

推荐答案

解决方案如下:

...

kv_str = Builder.load_string('''
#:import os os
...
        CustDrop:

            id: dropdown

            Button:
                text: 'Run another script'
                size_hint_y: None
                height: '48dp'
                on_release: os.system("python3 b.py")

输出

这篇关于如何从另一个python文件运行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆