Kivy的自我管理者得到屏幕-不工作



最近我尝试用python和kivy制作这个应用程序。经过数小时的谷歌搜索,我或多或少地拥有了我想要的一切。只缺少一件事:

我有几个屏幕。在应用程序的开头,我有一个菜单。之后我有很多问题;所有这些都有相同的文本和屏幕底部的按钮。我设法做到了以下几点:

<firstquestion>:
name: "firstquestion"
GridLayout:
cols: 1
size_hint_y: 1
GridLayout:
cols: 1
NeuLabelinBox:
text: "Here the Question"
GridLayout:
cols: 1
size_hint_y: 0.1
UnteresMenue:       

请注意"UnteresMenue",它指的是kv语言中的另一个类。我想我不知怎么把两个屏幕混在了一起。"UnteresMenue"的kv代码

<UnteresMenue>:
name: "UnteresMenue"
#id: UnteresMenue

GridLayout:
cols: 1
NeuButton: 
text: root.labeltext 

现在是python部分:

class UnteresMenue(Screen):
labeltext = StringProperty("Answer")

这很好。但现在我想更改"UnteresMenue"中标签的文本,当我按下应用程序开头的某个按钮(开始问题(时。"Untermenue"中的标签应更改为特定文本。所以要做到这一点:

text: "Exam"
self.manager.get_screen('UnteresMenue').labeltext = text

对于所有其他类,此方法都可以正常工作。但不适用于某个类"UnteresMenue"。是因为它是在问题中实现的,因此kv不承认它的性质吗?

如果有任何小提示,我将不胜感激!

这里有一个"迷你"示例:

在主菜单中,您可以进入"Exam"(检查(部分(示例中按此处(。进入本节(Prüfungsmodus(时,问题的性质应发生变化。再次按"此处"进入问题。

通常它在问题的顶部说"这应该改变",This应该在进入Prüfungsmodus屏幕时将标签更改为"Zeit"(python文件中的Class def on_enter(,但它没有。。。

Python文件:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, TransitionBase
from kivy.properties import ObjectProperty, NumericProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.core.text import LabelBase
from kivy.core.window import Window
from kivy.graphics import Color, InstructionGroup, Line, Rectangle
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.properties import ListProperty
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox
from kivy.uix.widget import Widget
from kivy.config import Config
from kivy.properties import StringProperty
from kivy.clock import Clock
from functools import partial
from kivy.uix.image import Image
from kivy.garden.navigationdrawer import NavigationDrawer as ND
from kivy.uix.scatter import Scatter
import random 
import time
Window.clearcolor = (0.2,0.2, 0.2,1)
Window.size = (480, 800)

#---------------------------------             Hauptmenü           -------------------------------------------
class hauptmenue(Screen, Widget):
pass
class Pruefungsmodus(Screen):

labeltext2 = StringProperty("hi")

def on_enter(self):
text = "Zeit" 
self.manager.get_screen('OberesMenue').labeltext = text  #DAS Klappt nicht
def StartPruefung(self):

sm.current = "ersteFrage"  

class UnteresMenue(Screen):
background_color_Kappa = ListProperty([0.2,0.2,0.2, 1]) 

class OberesMenue(Screen):
BildLabeltext = StringProperty("BilderLoesung_Bild_pressed.png")
BildLabeltext2 = StringProperty("BilderLoesung_Bild.png")
labeltext = StringProperty("This should change")

class ersteFrage(Screen):
pass


class WindowManager(ScreenManager):
pass


kv = Builder.load_file("my.kv")
sm = WindowManager()

sm.add_widget(hauptmenue(name="hauptmenue"))

sm.add_widget(Pruefungsmodus(name="Pruefungsmodus"))       
sm.add_widget(OberesMenue(name="OberesMenue"))    
sm.add_widget(UnteresMenue(name="UnteresMenue"))   
sm.add_widget(ersteFrage(name="ersteFrage"))  
sm.current = "hauptmenue"



class Vorbrereitung(App):
pruefung = ObjectProperty(None)
def build(self):
return sm
if __name__ == "__main__":
Vorbrereitung().run()

my.kv文件:



#:include alleFragen.kv
<NeuLabel2@Label>:

halign: "center" 
color:1,1,1,1 # <-----------
canvas.before:
Color:
rgba: 0.2,0.2, 0.2,1
Rectangle:
pos: self.pos
size: self.size  

<NeuLabelinBox@Label>:

font_size: "22sp"
color:0,0,0,1 # <-----------
canvas.before:
Color:
rgba: 0.949019608,  0.949019608,  0.949019608, 1
Rectangle:
pos: self.pos
size: self.size  
<NeuCheckBox@CheckBox>:
color: 0, 0, 0, 1
canvas.before:
Color:
rgba: 0.949019608,  0.949019608,  0.949019608, 1
Rectangle:
pos: self.pos
size: self.size  

<NeuButton@Button>:
font_size: "22sp"
background_normal: ''
#background_normal: "background.png"
background_color: 0.92549,0.92549,0.92549, 1
color:0,0,0,1 # <-----------
canvas.before:
Color:
rgba: 0.2,0.2, 0.2,1
Rectangle:
pos: self.pos
size: self.size   

<NeuButtonKappa@Button>:
font_size: "40sp"
background_normal: ''
background_color: 0.2,0.2,0.2, 1
color:1,1,1,1 # <-----------
canvas.before:
Color:
rgba: 0.2,0.2, 0.2,1
Rectangle:
pos: self.pos
size: self.size              
#----------------------------------            Hauptmenue          ---------------------------------------------------------------------------------
<hauptmenue>:
name: "Hauptmenue"
GridLayout:
cols:1
#spacing: 20

GridLayout:
cols:1
padding: 20
size_hint_y: 0.2           

NeuLabel2:
size_hint_x: 0.6
text: ""
font_size: (root.width**2 + root.height**2) / 13**4
halign: 'center'
valign: 'middle'

GridLayout:
cols:2
padding: 30
spacing: 20
size_hint_y: 0.5
BoxLayout:
NeuButton:
text: ''
BoxLayout:
NeuButton:
text: 'PRESS HERE'
on_release:
app.root.current = "Pruefungsmodus"
BoxLayout:
NeuButton:
text: ""
halign: 'center'
valign: 'middle'
BoxLayout:
NeuButton:
text: ""
BoxLayout:
padding: 10
size_hint_y: 0.15
NeuButton:
text: ''
#----------------------------------            Pruefungsmodus          ---------------------------------------------------------------------------------
<Pruefungsmodus>:
name: "Pruefungsmodus"
#background_color: 1, 1, 1, 1
id: pruefungsmudos
GridLayout:
cols:1
#spacing: 20

GridLayout:
cols:3
padding: 20
size_hint_y: 0.2           
Image:
size_hint_x: 0.2
source:"BilderLogo.png"   
NeuLabel2:
size_hint_x: 0.6
text: 'Pru00FCfungsnmodus'
font_size: (root.width**2 + root.height**2) / 12**4
halign: 'center'
valign: 'middle'
Image:
size_hint_x: 0.2
source:"BilderLogo2.png"     

GridLayout:
cols:1
padding: 30
spacing: 10
size_hint_y: 0.4
NeuLabelinBox:
id: text2
text: root.labeltext2
color: 0,0,0,1 # <-----------
canvas.before:
Color:
rgba: 0.949019608,  0.949019608,  0.949019608, 1
Rectangle:
pos: self.pos
size: self.size 
GridLayout:
cols:2
padding: 30
size_hint_y: 0.2    
BoxLayout:
padding: 30
spacing: 10
NeuButton:
size_hint_x: 0.5
text: "Press Here" 
on_release:
app.root.current = "ersteFrage"

NeuButton:
size_hint_x: 0.5
text: ""
on_release:
app.root.current = "hauptmenue"
#----------------------------------             Erste Frage           ---------------------------------------------------------------------------------
<ersteFrage>:
name: "ersteFrage"

GridLayout:
cols: 1
size_hint_y: 1
GridLayout:
cols: 1
size_hint_y: 0.08
OberesMenue:

GridLayout:
cols: 1
size_hint_y: 0.1 
NeuLabel2:
font_size: "18sp"
text: "Wie lautet......."        

GridLayout:
cols: 1

size_hint_y: 0.7
padding: 20
background_color: 0.92549,0.92549,0.92549, 1  
rows: 2
BoxLayout:
orientation:'horizontal'
NeuLabelinBox:
text: "h+u=ZU" 
NeuCheckBox:
NeuLabelinBox:
text: "h+u=ZU"                    
NeuCheckBox:
BoxLayout:
orientation:'horizontal'
NeuLabelinBox:
text: "h+u=ZU" 
NeuCheckBox:
NeuLabelinBox:
text: "h+u=ZU"
NeuCheckBox:
GridLayout:
cols: 1

size_hint_y: 0.1

UnteresMenue:      

以及外包的顶部和按钮菜单:alleFragen.kv

<OberesMenue>:
id: OberesMenueee
name: "OberesMenue"

GridLayout:
cols:1
GridLayout:
#size_hint_y: 0.05
spacing: 20
cols: 3
NeuButton:
text: ""
size_hint_x: 0.16
background_color: 1,1,1, 1
background_down:  root.BildLabeltext
background_normal:  root.BildLabeltext2
color:1,1,1,1 # <-----------
canvas.before:
Color:
rgba: 0.2,0.2, 0.2,1
Rectangle:
pos: self.pos
size: self.size  
NeuLabel2:
size_hint_x: 0.8 
text: root.labeltext
NeuButtonKappa: 
size_hint_x: 0.1 
text: "MENU"
background_down: "BilderHintergurndFarbe_app.png"
on_release:
app.root.current = "hauptmenue"

<UnteresMenue>:
name: "UnteresMenue"
id: UnteresMenuee

GridLayout:
cols: 5
spacing: 20
padding: 20
NeuButton: 
text: "<=="
NeuButton: 
text: "<"
NeuButtonKappa: 
text: 'k'
#background_color: root.background_color_Kappa
background_down:  "BilderHintergurndFarbe_app.png"
NeuButton: 
text: ">"
NeuButton: 
text: "==>"        

问题是您有两个不同的OberesMenue屏幕实例。

在您的python中创建了一个:

sm.add_widget(OberesMenue(name="OberesMenue"))

您在kv:中创建了另一个

<ersteFrage>:
name: "ersteFrage"

GridLayout:
cols: 1
size_hint_y: 1
GridLayout:
cols: 1
size_hint_y: 0.08
OberesMenue:

为了访问OberesMenue的第二个实例,您可以添加一个id:

<ersteFrage>:
name: "ersteFrage"

GridLayout:
cols: 1
size_hint_y: 1
GridLayout:
cols: 1
size_hint_y: 0.08
OberesMenue:
id: in_ersteFrage

然后,在Pruefungsmoduson_enter()方法中,可以使用更改该实例

def on_enter(self):
text = "Zeit"
self.manager.get_screen('ersteFrage').ids.in_ersteFrage.labeltext = text  #DAS Klappt nicht

请注意,这会更改OberesMenue的第二个实例中的Label,但对第一个实例没有影响。您的原始代码更改了第一个实例,但没有更改第二个实例。并且不能同时将同一个实例放在两个位置,因为Widget一次只能有一个父实例。

因此,如果这些实例是相同的,那么您需要更改两者中的Label

如果你真的想共享同一个实例,你可以自己记录它的位置,并使用remove_widget()add_widget()将它从一个父对象移动到另一个父节点。

最新更新