简体   繁体   English

Kivy:使用ScreenManager时如何在单独的屏幕中使用布局?

[英]Kivy: How do I use layouts within seperate screens while using ScreenManager?

I have some Kivy code which so far displays a Screen (BrickBreakerMenuScreen) with an Image and a Label, which, when pressed, changes to a blank Screen (GameScreen) with an image in the centre. 我有一些Kivy代码,到目前为止,该代码显示了一个带有图像和标签的屏幕(BrickBreakerMenuScreen),当按下该按钮时,将切换为一个中间有图像的空白屏幕(GameScreen)。 However, this is not the aim of my code; 但是,这不是我的代码的目的。 as you can see from the Builder, the second screen should align the Image in the top right corner: 从Builder中可以看到,第二个屏幕应该将Image对准右上角:

from kivy.lang import Builder
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.image import Image
from kivy import Config
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget
from kivy.animation import Animation
import time

Config.set('graphics', 'multisamples', '0')
Builder.load_string('''
<BrickBreakerMenuScreen>:
    BoxLayout:
        orientation: 'vertical'
        Image:
            source: 'brickbreaker log.png'
        Label:
            font_name: 'vgafix.fon'
            text: 'Tap to start'

<GameScreen>:
    AnchorLayout:
        anchor_x: 'right'
        anchor_y: 'top'
        Image:
            source: 'settings-cog.png'
''')

class BrickBreakerMenuScreen(Screen):
    def __init__(self, **kwargs):
        super(BrickBreakerMenuScreen, self).__init__(**kwargs)

    def on_touch_down(self, touch):
        sm.current = 'game'

class GameScreen(Screen):
    def __init__(self, **kwargs):
        super(GameScreen, self).__init__(**kwargs)

sm = ScreenManager(transition = FadeTransition())
sm.add_widget(BrickBreakerMenuScreen(name='menu'))
sm.add_widget(GameScreen(name='game'))

class BrickBreakerInsanityApp(App):
    def build(self):
        return sm



if __name__ == '__main__':
    BrickBreakerInsanityApp().run()

Is this a problem due to the ScreenManager? 这是由于ScreenManager引起的问题吗? I have changed the kv script to attempt to see if it's an issue with AnchorLayout, but none of the other layouts change the image's location on the GameScreen. 我更改了kv脚本以尝试查看AnchorLayout是否存在问题,但其他布局均未更改图像在GameScreen上的位置。

Any help is much appreciated. 任何帮助深表感谢。 Thanks! 谢谢!

Images: 图片:

来自GameScreen的齿轮

来自BrickBreakerMenuScreen的徽标

Your code is working OK. 您的代码运行正常。 The image is at the top-right corner. 该图像在右上角。
Your problem is the size of that Image widget. 您的问题是该Image小部件的大小。 Its as big as the size of the window. 它和窗口的大小一样大。
To fix this, you could add size_hint: .1, .1 to the Image properties and it will display as you want. 要解决此问题,您可以在Image属性中添加size_hint: .1, .1 ,它会根据需要显示。

    Image:
        size_hint: .1, .1
        source: 'settings-cog.png'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM