出于某种原因,在 kivy 中,当您创建一个屏幕类并向其添加一个小部件时,特别是一个图像,由于某种原因,与您创建一个小部件类并将该小部件类作为子类添加到屏幕类。这是我的 kv 文件代码:
<StartScreen>
# Start Screen
name:'Start'
orientation: 'vertical'
FloatLayout:
id: Start_Layout
Image:
id: Start_Background
source: r'Images\Battle.jpg'
keep_ratio: True
allow_stretch: True
size: root.size
<MainScreen>
name: 'Main'
orientation: 'vertical'
FloatLayout:
Image:
source: r'Images\Button.png'
allow_stretch: True
keep_ratio: False
size: 100, 100
对于python gui文件...
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.animation import Animation
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import *
from kivy.core.window import Window
from kivy.app import App
from kivy.lang import Builder
import kivy
kivy.require('1.9.1')
VERSION = '1.9.1'
class GenericButton(Widget):
Builder.load_file('Button.kv')
def __init__(self, **kwargs):
super(GenericButton, self).__init__(**kwargs)
self.Button = self.ids['Button']
self.size = Window.size
def on_touch_down(self, touch):
self.Button.source = r'Images\ButtonPressed.png'
def on_touch_up(self, touch):
self.Button.source = r'Images\Button.png'
class wid(Widget):
def __init__(self, **kwargs):
super(wid, self).__init__(**kwargs)
self.Button = Image(source='Images\Animatio\glow.gif', allow_stretch=False, keep_ratio=True) (假装它的缩进是新的,它不会让我将它添加到代码块中) self.add_widget (self.Button)
class StartScreen(Screen):
def __init__(self, **kwargs):
super(StartScreen, self).__init__(**kwargs)
#self.Layout = self.ids['Start_Layout']
#self.size = Window.size
#self.Layout.add_widget(GenericButton())
#self.ids['Start_Button'] = self.Layout.children[0]
print self.ids
#print self.ids.Start_Button.size
print self.size[0]/2, self.size[1]/2
class MainScreen(Screen):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.size = Window.size
def on_touch_down(self, touch):
self.Button.source = r'Images\ButtonPressed.png'
def on_touch_up(self, touch):
self.Button.source = r'Images\Button.png'
class ScreenManager(ScreenManager):
def __init__(self, **kwargs):
super(MCGMScreenManager, self).__init__(**kwargs)
Builder.load_file('Gui.kv')
self.add_widget(StartScreen())
self.add_widget(MainScreen())
该应用程序在我认为不需要发布的主文件中运行。但重要的一点可能是应用程序根类是 ScreenManager
编辑:我搞砸了一点,我在 python 中做了这个,但我清除了 GenericButton 的孩子,并添加了 GenericButton 作为 StartScreen 的孩子曾经拥有的按钮,同样的结果,一个巨大的无响应图像。