python - 从 python 代码而不是 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 文件中。当你show_popupw()
在MainWindow
类中调用函数时,你需要使用self
.
class MainWindow(Screen):
def set(self):
x=9
y=8
print(x,y)
self.show_popupw()
而且,您还需要show_popupw
使用 a 设置您的功能self
parameter
。因为您将通过它的self
for 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()
推荐阅读
- typescript - 从一个对象复制到另一个对象 - 仅来自其接口的公共属性的值
- reactjs - 具有全局状态管理的 next.js
- laravel - Laravel 在非直接相关值的 Pivot 上检索关系
- python - 在 VGG 神经网络中放置和获取大图像的策略
- amazon-web-services - 自动将数据从 S3 加载到 Redshift
- oracle - 如何在 oracle 中获取现有数据存储 CTXSYS(为索引创建)的定义?
- python-3.x - oauth2session dynamics365:不断收到错误 401 未授权
- javascript - 长度为 n 且 k 连续为 1 的二进制数组的数量
- postgresql - Nominatim - pgsql 返回错误代码 (3)
- r - R中空间缓冲区的for循环问题