【问题标题】:All elements rendering twice in Kivy + KivyMD所有元素在 Kivy + KivyMD 中渲染两次
【发布时间】:2020-07-06 09:22:28
【问题描述】:

我已经开始使用 Kivy 构建一个简单的应用程序(出于美学目的,我正在迁移到 KivyMD)并且遇到了一个问题,即我渲染到屏幕的所有元素都渲染了两次:第一次一次它们是静态且不可交互的,第二次是顶部可交互的。我正在使用一些带有按钮的 ScrollView,当我滚动时,下面的按钮是可见的。还有我更新的带有文本的标签,它们仍然在它们下面显示默认文本。

screenshot of double rendering elements

main.py

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.scrollview import ScrollView
from kivy.uix.togglebutton import ToggleButton
from kivy.properties import NumericProperty, ObjectProperty

class MagnateCalcApp(App):
    def build(self):
        return Builder.load_file("magnatecalc.kv")

class CalcWindow(Screen):
    current_value = NumericProperty(100)
    current_price = NumericProperty(1)
    pricelabel = ObjectProperty(None)
    valuelabel = ObjectProperty(None)
    money = ObjectProperty(None)

    def update(self):
        if self.current_price != 1:
            val = float(self.current_price)/1000
            self.pricelabel.text = "Price: " + str(val) + "M"
        if self.current_value != 100:
            self.valuelabel.text = "Multiplier: " + str(self.current_value)
        if self.current_price != 1 and self.current_value != 100:
            val = (float(self.current_value) * float(self.current_price))/1000
            print(val)
            self.money.text = "Value: " + str(val) + "M"

class RentWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

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

magnatecalc.kv(通常在 ScrollView 中有很多按钮,但为了便于阅读,其中大部分已被删除)

WindowManager:
    CalcWindow:
    RentWindow:

<Button>:
    font_size:35
    size_hint:0.5,0.5

<CalcWindow>:
    name: "Calc"
    current_value: 100
    current_price: 1
    pricelabel: pricelabel
    valuelabel: valuelabel
    money: money

    GridLayout:
        cols:2
        size: root.width, root.height

        Label:
            text:""
            pos_hint:{"x":0, "top":1}
            size_hint_y:0.15
            size_hint_x:0.05

        ScrollView:
            do_scroll_y:False
            do_scroll_x:True
            pos_hint:{"x":1, "top":1}
            size_hint_y:0.15
            GridLayout: # here i want a scrollview
                id: price
                rows: 1
                size_hint_x: None
                width: self.minimum_width
                ToggleButton:
                    text:"300K"
                    size_hint_x: None
                    on_press:
                        root.current_price = 300
                        root.update()
                    group:"price"
                ToggleButton:
                    text:"400K"
                    size_hint_x: None
                    on_press:
                        root.current_price = 400
                        root.update()
                    group:"price"
                ToggleButton:
                    text:"500K"
                    size_hint_x: None
                    on_press:
                        root.current_price = 500
                        root.update()
                    group:"price"
                ToggleButton:
                    text:"600K"
                    size_hint_x: None
                    on_press:
                        root.current_price = 600
                        root.update()
                    group:"price"
                ToggleButton:
                    text:"700K"
                    size_hint_x: None
                    on_press:
                        root.current_price = 700
                        root.update()
                    group:"price"
                ToggleButton:
                    text:"800K"
                    size_hint_x: None
                    on_press:
                        root.current_price = 800
                        root.update()
                    group:"price"
             
        ScrollView:
            do_scroll_y:True
            do_scroll_x:False
            pos_hint:{"x":0, "top":0}
            size_hint_x:0.075
            GridLayout: # here i want a scrollview
                id: multiplier
                cols: 1
                size_hint_y: None
                height: self.minimum_height
                ToggleButton:
                    text:"2"
                    size_hint_y: None
                    on_press:
                        root.current_value = 2
                        root.update()
                    group:"multiplier"
                ToggleButton:
                    text:"3"
                    size_hint_y: None
                    on_press:
                        root.current_value = 3
                        root.update()
                    group:"multiplier"
                ToggleButton:
                    text:"4"
                    size_hint_y: None
                    on_press:
                        root.current_value = 4
                        root.update()
                    group:"multiplier"
                ToggleButton:
                    text:"5"
                    size_hint_y: None
                    on_press:
                        root.current_value = 5
                        root.update()
                    group:"multiplier"
                ToggleButton:
                    text:"6"
                    size_hint_y: None
                    on_press:
                        root.current_value = 6
                        root.update()
                    group:"multiplier"
                ToggleButton:
                    text:"7"
                    size_hint_y: None
                    on_press:
                        root.current_value = 7
                        root.update()
                    group:"multiplier"
               
        GridLayout:
            rows:2
            pos_hint:{"x":1, "top":0}

            GridLayout:
                cols:2
                Label:
                    id:pricelabel
                    text:"Price: "
                    font_size:50
                Label:
                    id:valuelabel
                    text:"Multiplier: "
                    font_size:50
            GridLayout:
                cols:2
                Label:
                    id:money
                    text:"Value: "
                    font_size:90
                    size_hint_x:1
                    pos_hint:{"x":0, "top":0}

                Button:
                    text:"<- Rent"
                    pos_hint:{"x":1, "top":0}
                    size_hint: 0.25,0.25
                    on_release:
                        app.root.current = "Rent"
                        root.manager.transition.direction="right"

<RentWindow>:
    name: "Rent"
    Button:
        text:"Go Back ->"
        on_release:
            app.root.current="Calc"
            root.manager.transition.direction="left"

我的 .py 中是否有任何明显的缺失会导致这种情况?当我开始切换到 KivyMD 时出现了这个问题,但我已经恢复了这些更改以尝试恢复原始功能,但我无法在此过程中明确破坏某些内容。

【问题讨论】:

  • 我无法重现您的问题。你在运行什么操作系统?您使用的是什么版本的 kivy、kivymd 和 python?

标签: python kivy kivy-language


【解决方案1】:

您的 kv 文件被加载了两次,一次由您使用Builder.load_file 显式加载,一次是自动加载,因为它与您的应用类同名。

最简单的解决方案是跳过手动加载并删除您的 build 方法。

【讨论】:

  • 我们应该如何做到这一点,以及我们应该如何在没有build 方法的情况下构建我们的应用程序??
  • @YashasviBhatt 使用build方法没有错,这个答案是关于问题中具体不正确的build方法。
  • 这就是为什么我问你能告诉我如何在不使用构建方法的情况下做到这一点,因为我也有同样的问题,我想摆脱它,当我删除构建方法时屏幕变得完全空白
  • 然后发布一个具体问题,给出一个最小的可运行示例
【解决方案2】:

我遇到了和你一样的问题,我想出了如何正确构建应用程序以停止重复项目。 在 kivymd 中,这是构建应用程序的正确方法:

class MagnateCalcApp(MDApp):
    def build(self):
        return super().build()

我不知道如何,但它可以识别正确的 kivy 文件。

【讨论】:

    猜你喜欢
    • 2022-11-08
    • 2018-01-09
    • 2021-11-20
    • 2021-06-27
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多