我希望在单击按钮后打开新窗口。
我目前正在尝试的是创建另一个继承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()
答案 0 :(得分:1)
不建议在一个Kivy应用程序或Python脚本中包含两个App类。
我们将SettingScreen
分离到另一个Kivy应用程序中,并使用popen启动SettingScreen
。
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()
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()