Python 所有元素在Kivy+中渲染两次;基维姆

Python 所有元素在Kivy+中渲染两次;基维姆,python,kivy,kivy-language,Python,Kivy,Kivy Language,我已经开始使用Kivy构建一个简单的应用程序(为了美观起见,我正在迁移到KivyMD),并且遇到了一个问题,我要渲染到屏幕上的所有元素都渲染了两次:第一次是静态的、不可理解的,第二次是可交互的。我正在使用一些带有按钮的滚动视图,当我滚动时,下面的按钮是可见的。还有我更新的带有文本的标签,这些标签下面仍然显示默认文本 main.py import kivy from kivy.app import App from kivy.lang import Builder from kivy.uix.l

我已经开始使用Kivy构建一个简单的应用程序(为了美观起见,我正在迁移到KivyMD),并且遇到了一个问题,我要渲染到屏幕上的所有元素都渲染了两次:第一次是静态的、不可理解的,第二次是可交互的。我正在使用一些带有按钮的滚动视图,当我滚动时,下面的按钮是可见的。还有我更新的带有文本的标签,这些标签下面仍然显示默认文本

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:
租赁窗口:
:
字体大小:35
尺寸提示:0.5,0.5
:
名称:“Calc”
当前_值:100
现行价格:1
价格标签
valuelabel:valuelabel
钱:钱
网格布局:
科尔斯:2
尺寸:根.宽度,根.高度
标签:
案文:“”
pos_提示:{“x”:0,“top”:1}
尺寸:0.15
尺寸提示:0.05
滚动视图:
是否滚动y:错误
是否滚动\u x:True
pos_提示:{“x”:1,“top”:1}
尺寸:0.15
GridLayout:#这里我想要一个滚动视图
id:价格
行数:1
大小提示:无
宽度:自最小宽度
切换按钮:
文本:“300K”
大小提示:无
新闻界:
root.current\u价格=300
root.update()
组:“价格”
切换按钮:
文本:“400K”
大小提示:无
新闻界:
root.current_价格=400
root.update()
组:“价格”
切换按钮:
正文:“500K”
大小提示:无
新闻界:
root.current_价格=500
root.update()
组:“价格”
切换按钮:
正文:“600K”
大小提示:无
新闻界:
root.current\u价格=600
root.update()
组:“价格”
切换按钮:
文本:“700K”
大小提示:无
新闻界:
root.current\u价格=700
root.update()
组:“价格”
切换按钮:
正文:“800K”
大小提示:无
新闻界:
root.current\u价格=800
root.update()
组:“价格”
滚动视图:
是否滚动y:正确
是否滚动\u x:False
pos_提示:{“x”:0,“top”:0}
尺寸:0.075
GridLayout:#这里我想要一个滚动视图
id:乘数
科尔斯:1
尺寸提示:无
高度:自身最小高度
切换按钮:
案文:“2”
尺寸提示:无
新闻界:
root.current_值=2
root.update()
组:“乘数”
切换按钮:
案文:“3”
尺寸提示:无
新闻界:
root.current_值=3
root.update()
组:“乘数”
切换按钮:
案文:“4”
尺寸提示:无
新闻界:
root.current_值=4
root.update()
组:“乘数”
切换按钮:
案文:“5”
尺寸提示:无
新闻界:
root.current_值=5
root.update()
组:“乘数”
切换按钮:
案文:“6”
尺寸提示:无
新闻界:
root.current_值=6
root.update()
组:“乘数”
切换按钮:
案文:“7”
尺寸提示:无
新闻界:
root.current_值=7
root.update()
组:“乘数”
网格布局:
行数:2
pos_提示:{“x”:1,“top”:0}
网格布局:
科尔斯:2
标签:
id:价格标签
文字:“价格:”
字体大小:50
标签:
id:valuelabel
文本:“乘数:”
字体大小:50
网格布局:
科尔斯:2
标签:
id:钱
文本:“值:”
字体大小:90
大小提示:1
pos_提示:{“x”:0,“top”:0}
按钮:
案文:“”
发布时:
app.root.current=“计算”
root.manager.transition.direction=“left”

在我的.py中是否有任何明显的遗漏会导致这种情况?当我开始切换到KivyMD时出现了这个问题,但后来我恢复了这些更改,试图恢复到原始功能,我不能因此而明确地破坏了某些东西。

您的kv文件正在加载到tw
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"