我希望有几个Screen
,每个都有不同的尺寸,但我不能。每次我尝试更改一个屏幕的大小时,Kivy都会更改所有屏幕的大小
我该怎么办?
我一直在尝试使用kivy.core.window.Window
和Window.size = (x,y)
,但它会改变所有屏幕的大小,即使我在每个屏幕的__init__
函数内写入该行,因为它们在加载时到ScreenManager
,__init__
也已加载。
.py
示例:from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window
class Screen_Manager(ScreenManager):
pass
class Main(Screen):
def __init__(self, **kwargs):
super(Main, self).__init__(**kwargs)
Window.size = (900, 500)
class Login(Screen):
def __init__(self, **kwargs):
super(Login, self).__init__(**kwargs)
Window.size = (400, 300)
class MyApp(App):
def build(self):
return Screen_Manager()
MyApp().run()
<Screen_Manager>:
id: screen_manager
Main:
Login:
<Login>:
name: 'login'
Button:
text: 'Go to Main'
on_press: root.manager.current = 'main'
<Main>:
name: 'main'
Button:
text: 'Go to Login'
on_press: root.manager.current = 'login'
.kv
#:import Window kivy.core.window.Window
<Screen_Manager>:
[...]
<Login>:
[...]
Button:
[...]
on_press:
Window.size = (900, 400)
root.manager.current = 'main'
<Main>:
[...]
Button:
[...]
on_press:
Window.size = (400, 300)
root.manager.current = 'login'
但是......我不太喜欢......
答案 0 :(得分:1)
您绝对不希望屏幕必须知道要转移到的屏幕大小。我做了一些研究,你只需要在每个屏幕中添加一个函数(on_enter或on_pre_enter)。将此用于.py程序:
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window
class Screen_Manager(ScreenManager):
pass
class Main(Screen):
def __init__(self, **kwargs):
super(Main, self).__init__(**kwargs)
def on_pre_enter(self):
Window.size = (900, 500)
class Login(Screen):
def __init__(self, **kwargs):
super(Login, self).__init__(**kwargs)
def on_pre_enter(self):
Window.size = (400, 300)
class MyApp(App):
def build(self):
return Screen_Manager()
MyApp().run()
如果你计划做很多不同大小的屏幕,你可能会更酷并创建一个包含默认大小和on_pre_enter方法的Screen子类。