[英]Switching between screens from python code not KV
我是 Kivy 的新手 我想管理 Python 中屏幕之間的更改,而不是 KV 我在 Python 中編寫了一個函數,當在彈出窗口中按下按鈕時,屏幕將更改為第二個屏幕,正如您在終端的值似乎是正確的[與以前的版本不同,我得到了一個錯誤,第二屏沒有這樣的名稱] 我做錯了什么? 為什么該功能沒有切換到第二個屏幕?
PY
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty ,NumericProperty
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
class MainWindow(Screen):
def set(self):
x=9
y=8
print(x,y)
show_popupw()
def change_screen(self):
print("c_S")
self.sm = ScreenManager()
self.sm.add_widget(SecondWindow(name='second'))
self.sm.current = 'second'
print(self.sm)
print(self.sm.current)
def show_popupw():
show = Pow()
popupWindow = Popup(title="Popup Window", content=show, size_hint=(None,None),size=(400,400))
popupWindow.open()
class SecondWindow(Screen):
a = NumericProperty()
def sec(self):
self.a = self.a + 1
print(self.a)
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('mymainsctaq.kv')
class Pow(FloatLayout):
mainw=MainWindow()
class mymainscreenApp(App):
def build(self):
return kv
if __name__ == "__main__":
mymainscreenApp().run()
千伏
WindowManager:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
GridLayout:
cols:2
GridLayout:
cols: 4
Label:
text: "name: "
TextInput:
id: name
multiline: False
Button:
text: "Submit"
on_release:
app.root.current = "second" if name.text == "go" else "main"
root.manager.transition.direction = "left"
Button:
text: "set"
on_release:
root.set()
<SecondWindow>:
name: 'second'
GridLayout:
cols:2
GridLayout:
cols: 4
Label:
text: "num: " + str(root.a)
Button:
text: "Go Back"
on_press:
app.root.current = "main"
root.manager.transition.direction = "up"
root.sec()
<Pow>:
Label:
text: "You set?"
size_hint: 0.6, 0.2
pos_hint: {"x":0.2, "top":1}
Button:
text: "ok,set"
size_hint: 0.8, 0.2
pos_hint: {"x":0.1, "y":0.1}
on_release:
root.mainw.change_screen()
在python中從同一個類調用函數時,您需要使用self
指定您是從同一個類調用該函數。
所以在你的代碼中的 .py 文件中。 當您在MainWindow
類中調用函數show_popupw()
,您需要使用self
。
class MainWindow(Screen):
def set(self):
x=9
y=8
print(x,y)
self.show_popupw()
而且,您還需要使用self
parameter
設置show_popupw
函數。 因為你會傳遞它的self
以供reference
。
def show_popupw(self):
show = Pow()
popupWindow = Popup(title="Popup Window",
content=show,
size_hint=(None,None),
size=(400,400))
popupWindow.open()
您的 .py 文件應如下所示:
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty ,NumericProperty
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
class MainWindow(Screen):
def set(self):
x=9
y=8
print(x,y)
self.show_popupw()
def change_screen(self):
print("c_S")
self.sm = ScreenManager()
self.sm.add_widget(SecondWindow(name='second'))
self.sm.current = 'second'
print(self.sm)
print(self.sm.current)
def show_popupw(self):
show = Pow()
popupWindow = Popup(title="Popup Window",
content=show,
size_hint=(None,None),
size=(400,400))
popupWindow.open()
class SecondWindow(Screen):
a = NumericProperty()
def sec(self):
self.a = self.a + 1
print(self.a)
class WindowChanger(ScreenManager):
pass
class Pow(FloatLayout):
mainw=MainWindow()
kv = Builder.load_file('mymainsctaq.kv')
class mymainscreenApp(App):
def build(self):
return kv
if __name__ == "__main__":
mymainscreenApp().run()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.