如何在猕猴桃中打开按钮按下事件的新窗口?

时间:2019-06-15 09:55:00

标签: python-3.x kivy kivy-language

我希望在单击按钮后打开新窗口。 我目前正在尝试的是创建另一个继承kivy.App的类的实例。

class SettingScreen(App):
    pass

class Screen(Widget):
    btn = ObjectProperty(None)
    def btn_touch_up(self):
       print("Touch Up ")
       SettingScreen().run()

class TouchApp(App):
def build(self):
    return Screen()

if __name__ == "__main__":
   TouchApp().run()

单击按钮后发生的事情是我的窗口标题发生了变化,但是“窗口并没有变空白。那是我现有的小部件仍在窗口中

touch.ky

<Screen>:
btn:btn
Label:
    id: msg
    text: "Hii"
    color: 1,0,0,1

    pos_hint: {"top":0.8}
Button:
    id: btn
    size_hint: 0.2,0.2
    text: "Touch Me"
    on_touch_up: root.btn_touch_up()

1 个答案:

答案 0 :(得分:1)

不建议在一个Kivy应用程序或Python脚本中包含两个App类。

我们将SettingScreen分离到另一个Kivy应用程序中,并使用popen启动SettingScreen

示例

touch.py​​

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen
from kivy.properties import ObjectProperty
from kivy.lang import Builder


Builder.load_string("""
<Screen>:
    btn:btn
    orientation: 'vertical'
    Label:
        id: msg
        text: "Hi"
        color: 1,0,0,1
        pos_hint: {"top":0.8}

    Button:
        id: btn
        size_hint: 0.2,0.2
        text: "Touch Me"
        on_release:  root.btn_touch_up()

""")


class Screen(BoxLayout):
    btn = ObjectProperty(None)

    def btn_touch_up(self):
        print("Touch Up ")
        from subprocess import Popen, PIPE
        process = Popen(['python3', 'settings.py'], stdout=PIPE, stderr=PIPE)


class TouchApp(App):
    def build(self):
        return Screen()


if __name__ == "__main__":
    TouchApp().run()

settings.py

from kivy.app import App
from kivy.lang import Builder


kv = Builder.load_string("""
Screen:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Setting Screen'
        Button:
            text: 'Exit'
            on_release: app.stop()
""")


class SettingScreen(App):

    def build(self):
        return kv


if __name__ == "__main__":
    SettingScreen().run()

输出

Result - 2 Kivy Apps running