将小部件动态添加到kivy屏幕对象中

时间:2016-12-03 04:53:53

标签: python kivy

据我所知,kv语言在制作静态显示时非常有用,例如:而不是在运行时需要很多小部件定位的游戏。在这里,我尝试制作一个简单的游戏,但仍需要很多定位,因此kv语言不适用于小部件,但不适用于屏幕。我使用屏幕来区分主菜单和游戏屏幕。但是当我尝试使用'add_widget'来插入我的图像时,它总是位于窗口的中间。后来我发现屏幕尺寸只有100x100。

以下是我能想到的唯一方法,但仍然没有运气:

class HomeScreen(Screen):
    pass

class GameScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

presentation=Builder.load_file('ProjectSD.kv')

class ProjectSDApp(App):
    def build(self):
        A=presentation
        A.screens[0].size=(Window.size)
        A.screens[0].add_widget(Label(text='hello',font_Size=80,pos=(0,0)))
        return A

if __name__=='__main__':
    print(Window.size)
    ProjectSDApp().run()

和我的ProjectSD.kv文件:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
    transition: FadeTransition()
    HomeScreen:
    GameScreen:
<Button>:
    font_name:'attackofthecucumbers.ttf'

<HomeScreen>:
    name:'home'
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'nature.jpg'
    Label:
        text: 'Monopoly GX'
        font_name:'KBDunkTank.ttf'
        font_size:100
        size_hint:0.7,0.2
        pos:root.width*0.15,root.height*0.70
    Button:
        on_release: app.root.current = "game"
        text: 'Play Game'
        font_size:root.width/20
        size_hint:0.3,0.15
        pos:root.width*0.35,root.height*0.45
    Button:
        on_release: app.stop()
        text: 'exit'
        font_size:root.width/20
        size_hint:0.3,0.15
        pos:root.width*0.35,root.height*0.20

<GameScreen>:
   name:'game'
   Button:
      on_release: app.root.current = "home"
      background_color: (1,0.15,0.2,0.8)
      text: 'X'
      font_size:root.width/40
      size_hint:0.05,0.05
      pos:root.width*0.95,root.height*0.95

由于屏幕对象中没有'pos'方法,我手动将我的小部件放到位置(0,0)。

我找到的唯一方法就在下面: https://kivyspacegame.wordpress.com/2014/08/10/tutorial-flappy-ship-part-2-build-simple-menus-and-animate-your-games-using-clock/

所以我的问题是,如果我使用kivy内置的屏幕对象,如何实现相同的结果?所以我仍然可以在以后添加和删除小部件?

1 个答案:

答案 0 :(得分:0)

我并不完全清楚你在问什么;我在代码中的任何地方都没有看到您尝试将图像添加到布局的位置。你可以在中间添加一个标签,并且工作正常。

我认为因为您创建的屏幕没有任何布局而没有设置默认窗口大小,所以屏幕只占用其最小默认大小。您需要添加布局并使用具有已定义大小的内容填充它,或者在开头设置窗口大小,例如:

# window import
from kivy.core.window import Window
from kivy.utils import get_color_from_hex
Window.size = (1920/2,1080/2)
Window.clearcolor = get_color_from_hex('#000000') # black

以下是一些创建两个Screen的代码,并在Image的位置添加了一个可拖动的FloatLayout

from kivy.app import App
from kivy.uix.screenmanager import (ScreenManager, Screen)
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.behaviors import DragBehavior
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout

class HomeScreen(Screen):
    pass

class GameScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

class MovingImage(DragBehavior,Image):
    pass

class DragLabel(DragBehavior, Label):
    pass

class StaticImage(Image):
    pass

class MyApp(App):
    def build(self):
        A = ScreenManagement()
        A.current = 'game'
        A.screens[1].ids.gamefloat.add_widget(MovingImage(size_hint = [0.3,0.3]))
        A.screens[1].ids.gamefloat.add_widget(StaticImage(pos = (150,300)))
        return A

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

和.kv文件名为myapp.kv

<ScreenManagement>:
    HomeScreen:
    GameScreen:

<Button>:

<HomeScreen>:
    name:'home'
    canvas:
        Rectangle:
            pos: self.pos
            size: self.size
            source: 'image.jpg'
    Label:
        text: 'Monopoly GX'
        font_size:100
        size_hint:0.7,0.2
        pos:root.width*0.15,root.height*0.70
    Button:
        on_release: app.root.current = "game"
        text: 'Play Game'
        font_size:root.width/20
        size_hint:0.3,0.15
        pos:root.width*0.35,root.height*0.45
    Button:
        on_release: app.stop()
        text: 'exit'
        font_size:root.width/20
        size_hint:0.3,0.15
        pos:root.width*0.35,root.height*0.20

<GameScreen>:
    name:'game'
    FloatLayout:
        id: gamefloat
        Button:
            on_release: app.root.current = "home"
            background_color: (1,0.15,0.2,0.8)
            text: 'X'
            font_size:root.width/40
            size_hint:0.05,0.05
            pos:root.width*0.95,root.height*0.95

<MovingImage>:
    drag_rectangle: self.x, self.y, self.width, self.height
    drag_timeout: 1000000
    drag_distance: 0
    source: 'image.jpg'

<StaticImage>:
    source: 'image.jpg'