按钮和标签文字未显示

时间:2019-05-25 15:11:53

标签: python kivy kivy-language

Pictures Here

图片中的两个窗口是精确的副本(相同的代码行),唯一的不同是右窗口是屏幕管理器内部的屏幕,但右窗口却不显示文本。

我不确定是什么问题,并且在进行研究时找不到与之相关的任何东西。

右侧窗口PYTHON文件:

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.config import Config
space = ""

x = [(a * 9), (a*16)]
Config.set('graphics', 'width', x[0])
Config.set('graphics', 'height', x[1])

class MainScreen(Screen):
    pass

class SecondScreen(Screen):
    pass

class ThirdScreen(Screen):
    pass


kv = Builder.load_file("main2.kv")
class ComplimentUI(App):
    def build(self):
        return kv
    def change_screen(self, x):
        scrnmanager = self.root.ids['sm']
        scrnmanager.current = x

if __name__ == "__main__":
    ComplimentUI().run()

右侧窗口构建器文件:(main2.kv)

#:include secondscreen.kv
#:include thirdscreen.kv

<MainScreen>:
    canvas.before:
         Color:
            rgb: .59, .74, .20
        Rectangle:
            size: self.size
            pos: self.pos
        FloatLayout:
            TextInput:
                pos_hint: {'center_x':1, 'top':.1}
                size_hint: .1,.1
                multiline: False
                font_size: 20
                font_name: "Dimbo Regular"
                background_normal: ''
                background_color: 0,0,0,0

            Button:
                text: "PLAY!"
                pos_hint: {'center_x':.5, 'center_y':.3}
                size_hint: .8, .17
                font_name: "Splatch"
                color: 0.15, .5, 0.2, 1
                font_size: 0.14 * self.width
                background_normal: ''
                background_color: 0,0,0,0
                on_press:
                print("pressed, navigating to prev screen")
                app.change_screen("screentwo")
                root.manager.transition.direction = "left"
                canvas.before:
                    Color:
                        rgb: 1,1, .8,1
                    RoundedRectangle:
                        size: self.size
                        pos: self.pos
                        radius: [4,]

ScreenManager:
    GridLayout:
        cols: 1
        ScreenManager:
            id: sm
        MainScreen:
            name: "screenone"
            id: screenone
        SecondScreen:
            name: "screentwo"
            id: screentwo
        ThirdScreen:
            name: "thirdscreen"
            id: thirdscreen[enter image description here][1]

左侧的窗口Python文件:

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.config import Config
space = "";

x = [(a * 9), (a*16)]
Config.set('graphics', 'width', x[0])
Config.set('graphics', 'height', x[1])

kv = Builder.load_file("main.kv")
class MyMainApp(App):
    def build(self):
        return kv

if __name__ == "__main__":
    MyMainApp().run()

左窗口构建器文件:(main.kv)

FloatLayout:
    canvas.before:
        Color:
            rgb: .59, .74, .20
        Rectangle:
            size: self.size
            pos: self.pos
    TextInput:
        pos_hint: {'center_x':1, 'top':.1}
        size_hint: .1,.1
        multiline: False
        font_size: 20
        font_name: "Dimbo Regular"
        background_normal: ''
        background_color: 0,0,0,0

    Button:
        text: "PLAY!"
        pos_hint: {'center_x':.5, 'center_y':.3}
        size_hint: .8, .17
        font_name: "Splatch"
        color: 0.15, .5, 0.2, 1
        font_size: 0.14 * self.width
        background_normal: ''
        background_color: 0,0,0,0
        on_press:
            print("pressed, navigating to prev screen")
            app.change_screen("screentwo")
            root.manager.transition.direction = "left"
        canvas.before:
            Color:
                rgb: 1,1, .8,1
            RoundedRectangle:
                size: self.size
                pos: self.pos
                radius: [4,]

新编辑:

正如我在下面的注释中所指出的,如果将secondscreen.kvthirdscreen.kv的FloatLayout放到左侧窗口的构建器文件中,同样的问题也会发生,这是正常的。但是,当我将其放在屏幕管理器(如右窗口)内的屏幕中时,它消失了。我的所有屏幕都遇到相同的问题。我之所以只放mainscreen.kv的原因是因为它是所有屏幕中最短的,但是我仍然会提供文件,但是它只有1个版本(用于右侧窗口的版本),因为它们是基本一致。 (唯一的区别是为其类名称添加了第一行,并进行了略微的更改以适应各个级别)

次屏幕构建器文件:

<SecondScreen>:
    FloatLayout:
        canvas.before:
            Color:
                rgb: .59, .74, .20
            Rectangle:
                size: self.size
                pos: self.pos
        TextInput:
            pos_hint: {'center_x':.5, 'center_y':.56}
            size_hint: .8, .3
            multiline: False
            font_size: 20
            font_name: "Dimbo Regular"
            background_normal: ''
            background_color: 1, 1, .8, 1

        Label:
            pos_hint: {'center_x':.5, 'center_y':.76}
            size_hint: .8, .07
            text: "NAME!"
            font_size: 0.8 * self.height
            font_name: "kidsrock"
            color: 0.15, .5, 0.2, 1
            background_color: 0,0,0,0
            canvas.before:
                Color:
                    rgb: 1,1, .8,1
                RoundedRectangle:
                    size: self.size
                    pos: self.pos
                    radius: [4,]

        Button:
            text: "<<<"
            size_hint: .28, .07
            pos_hint: {'center_x':.24, 'center_y':.1}
            font_name: "kidsrock"
            color: 0.15, .5, 0.2, 1
            font_size: 0.35 * self.width
            background_normal: ''
            background_color: 0,0,0,0
            on_press:
                print("pressed, navigating to prev screen")
                app.change_screen("screenone")
                root.manager.transition.direction = "right"
            canvas.before:
                Color:
                    rgb: 1,1, .8,1
                RoundedRectangle:
                    size: self.size
                    pos: self.pos
                    radius: [4,]
        Button:
            pos_hint: {'center_x':.8, 'center_y':.35}
            size_hint: .2, .07
            background_normal: ''
            background_color: 0,0,0,0
            color: 0.15, .5, 0.2, 1
            text: "ENTER"
            font_name: "kidsrock"
            font_size: 0.25 * self.width
            on_press:
                print("pressed, navigating to next screen")
                app.change_screen("thirdscreen")
                root.manager.transition.direction = "left"
            canvas.before:
                Color:
                    rgb: 1,1, .8,1
                RoundedRectangle:
                    size: self.size
                    pos: self.pos
                    radius: [4,]

第三方屏幕构建器文件:

<ThirdScreen>:
    FloatLayout:
        canvas.before:
            Color:
                rgb: .59, .74, .20
            Rectangle:
                size: self.size
                pos: self.pos
        TextInput:
            pos_hint: {'center_x':1, 'top':.1}
            size_hint: .1, .1
            multiline: False
            font_size: 20
            font_name: "Dimbo Regular"
            background_normal: ''
            background_color: 0, 0, 0, 0

        Label:
            pos_hint: {'center_x':.5, 'center_y':.71}
            size_hint: .8, .17
            text: "COMPLIMENT!"
            font_size: 20
            font_name: "kidsrock"
            color: 0.15, .5, 0.2, 1
            background_color: 0,0,0,0
            canvas.before:
                Color:
                    rgb: 1,1, .8,1
                RoundedRectangle:
                    size: self.size
                    pos: self.pos
                    radius: [4,]

        Button:
            pos_hint: {'center_x':.24, 'center_y':.1}
            size_hint: .28, .07
            font_name: "kidsrock"
            color: 0.15, .5, 0.2, 1
            font_size: 0.35 * self.width
            text: "<<<"
            background_normal: ''
            background_color: 0,0,0,0
            on_press:
                print("pressed, navigating to prev screen")
                app.change_screen("screentwo")
                root.manager.transition.direction = "right"
            canvas.before:
                Color:
                    rgb: 1,1, .8,1
                RoundedRectangle:
                    size: self.size
                    pos: self.pos
                    radius: [4,]

NEW- NEW EDIT!:

更新 重新启动计算机后,现在看起来像这样。

2 个答案:

答案 0 :(得分:0)

根本原因-按钮和标签文本不显示

最可能的原因是文本的默认color为白色([1、1、1、1]),并且两个小部件的背景颜色也均为白色。您需要将文字颜色设置为非白色,例如黑色([0,0,0,1])。

TextInput框-未显示

由于background_normal: ''background_color: 0,0,0,0,两个窗口(左和右)的TexInput框都不可见。

注释掉它们,然后将显示TextInput框。

按钮的文本“ PLAY”-在右侧窗口中不显示

可能是包含kv文件中的内容,例如secondscreen.kv和/或thirdscreen.kv正在引起可见性。注释掉两个kv文件,以解决问题。

示例

下面的右窗口示例,即使用ScreenManager可以显示Button的文本,“ PLAY”以及TextInput框。

main-right.py

from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder


class MainScreen(Screen):
    pass


class SecondScreen(Screen):
    pass


class ThirdScreen(Screen):
    pass


kv = Builder.load_string("""
# :include secondscreen.kv
# :include thirdscreen.kv

<MainScreen>:
    canvas.before:
        Color:
            rgb: .59, .74, .20
        Rectangle:
            size: self.size
            pos: self.pos
    FloatLayout:
        TextInput:
            hint_text: 'Type here'
            pos_hint: {'center_x':1, 'top':.1}
            size_hint: .1,.1
            multiline: False
            font_size: 20
            # font_name: "Dimbo Regular"
            # background_normal: ''
            # background_color: 0,0,0,0

        Button:
            text: "PLAY!"
            pos_hint: {'center_x':.5, 'center_y':.3}
            size_hint: .8, .17
            #font_name: "Splatch"
            color: 0.15, .5, 0.2, 1
            font_size: 0.14 * self.width
            background_normal: ''
            background_color: 0,0,0,0
            on_press:
                print("pressed, navigating to prev screen")
                app.change_screen("screentwo")
                root.manager.transition.direction = "left"
            canvas.before:
                Color:
                    rgba: 1, 1, .8, 1
                RoundedRectangle:
                    size: self.size
                    pos: self.pos
                    radius: [4,]

GridLayout:
    cols: 1
    ScreenManager:
        id: sm
        MainScreen:
            name: "screenone"
            id: screenone
        SecondScreen:
            name: "screentwo"
            id: screentwo
        ThirdScreen:
            name: "thirdscreen"
            id: thirdscreen
""")


class RightWindow(App):
    title = "ComplimentUI"

    def build(self):
        return kv


if __name__ == "__main__":
    RightWindow().run()

输出

Result

答案 1 :(得分:0)

我遇到了类似的问题,结果证明我的错误是按钮的 size_hint_x 声明。一旦我删除了它,文本就可见了。