【发布时间】: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