Kivy 屏幕管理器的问题 Python
Problems with Kivy Screen Manager Python
我一直在互联网上搜索修复程序,但我似乎无法修复它..
我想制作一个游戏,其中当然有一个菜单和播放屏幕,但是按钮非常大,尽管“大小:50、50”和“位置:300、200”
main.py:
from kivy.app import App
from kivy.uix.widget import Widget
#from kivy.core.window import Window
#from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.config import Config
from kivy.core.image import Image
from kivy.graphics import BorderImage
from kivy.graphics import Color, Rectangle
from kivy.uix.image import AsyncImage
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen, CardTransition
import time
import random
# Config.set('graphics', 'width', '1000')
# Config.set('graphics', 'height', '800')
class GridButtons(GridLayout):
pass
class screen1(Screen):
pass
class screen2(Screen):
pass
class GameApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(screen1(name='menu'))
sm.add_widget(screen2(name='game'))
return sm
if __name__ == '__main__':
GameApp().run()
kivy.kv
#kivy 2.0.0
<screen1>:
orientation: 'vertical'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'gamebg.jpg'
GridButtons:
cols: 1
rows: 1
Button:
size: 2, 2
pos: 10, 20
text: 'Play'
The screen i'm getting
我想要屏幕中间的播放按钮,并自动调整大小
如果您想在布局中设置所需大小的小部件,您必须禁用 size_hint
属性。因此你需要的改变是,
Button:
size_hint: None, None
size: 2, 2
I want the Play Button in the middle of the screen..
因为您直接在 GridLayout
中添加 Button
并且它会自动管理其子项的位置(默认情况下 lr-tb
表示从左到右,从上到下;从版本 2.0.0 ), pos
或 pos_hint
的 none 将按预期工作。
要实现这一点,您可以将其添加到 FloatLayout
中,如,
FloatLayout:
Button:
size_hint: None, None
size: 2, 2
pos_hint: {"center_x" : 0.5, "center_y" : 0.5}
我一直在互联网上搜索修复程序,但我似乎无法修复它..
我想制作一个游戏,其中当然有一个菜单和播放屏幕,但是按钮非常大,尽管“大小:50、50”和“位置:300、200”
main.py:
from kivy.app import App
from kivy.uix.widget import Widget
#from kivy.core.window import Window
#from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.config import Config
from kivy.core.image import Image
from kivy.graphics import BorderImage
from kivy.graphics import Color, Rectangle
from kivy.uix.image import AsyncImage
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen, CardTransition
import time
import random
# Config.set('graphics', 'width', '1000')
# Config.set('graphics', 'height', '800')
class GridButtons(GridLayout):
pass
class screen1(Screen):
pass
class screen2(Screen):
pass
class GameApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(screen1(name='menu'))
sm.add_widget(screen2(name='game'))
return sm
if __name__ == '__main__':
GameApp().run()
kivy.kv
#kivy 2.0.0
<screen1>:
orientation: 'vertical'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'gamebg.jpg'
GridButtons:
cols: 1
rows: 1
Button:
size: 2, 2
pos: 10, 20
text: 'Play'
The screen i'm getting
我想要屏幕中间的播放按钮,并自动调整大小
如果您想在布局中设置所需大小的小部件,您必须禁用 size_hint
属性。因此你需要的改变是,
Button:
size_hint: None, None
size: 2, 2
I want the Play Button in the middle of the screen..
因为您直接在 GridLayout
中添加 Button
并且它会自动管理其子项的位置(默认情况下 lr-tb
表示从左到右,从上到下;从版本 2.0.0 ), pos
或 pos_hint
的 none 将按预期工作。
要实现这一点,您可以将其添加到 FloatLayout
中,如,
FloatLayout:
Button:
size_hint: None, None
size: 2, 2
pos_hint: {"center_x" : 0.5, "center_y" : 0.5}