【发布时间】:2019-07-13 19:43:39
【问题描述】:
我正在尝试编写代码来计算粉刷整个房间需要多少油漆。
我的编码:
# main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from database import DataBase
from kivy.properties import NumericProperty
class CreateAccountWindow(Screen):
panjang = ObjectProperty(None)
lebar = ObjectProperty(None)
tinggi = ObjectProperty(None)
def submit(self):
if self.tinggi.text !="":
try:
if float(self.tinggi.text) and float(self.lebar.text) and float(self.panjang.text):
sm.current = "main"
except:
invalidForm()
else:
invalidForm()
class MainWindow(Screen):
# Each screen has by default a property manager that gives
# you the instance of the ScreenManager used.
# declare class attributes
panjang = ObjectProperty(None)
lebar = ObjectProperty(None)
tingi = ObjectProperty(None)
luas1 = ObjectProperty(None)
luas2 = ObjectProperty(None)
cat = ObjectProperty(None)
def logOut(self):
self.manager.current = "create"
def volume(self):
luas1 = float(self.manager.ids.create.panjang.text) * float(self.manager.ids.create.tinggi.text) / 10
luas2 = float(self.manager.ids.create.lebar.text) * float(self.manager.ids.create.tinggi.text) / 10
self.luas1.text = str(luas1)
self.luas2.text = str(luas2)
self.cat.text = str(luas1 * 2 + luas2 * 2)
def on_enter(self, *args):
self.volume() # calculate volume
self.panjang.text = "Panjang: " + self.manager.ids.create.panjang.text
self.lebar.text = "Lebar: " + self.manager.ids.create.lebar.text
self.tinggi.text = "Tinggi: " + self.manager.ids.create.tinggi.text
self.luas1.text = "Luas:" + self.manager.ids.main.luas1.text
self.luas2.text = "Luas:" + self.manager.ids.main.luas2.text
self.cat.text = "Luas:" + self.manager.ids.main.cat.text
class WindowManager(ScreenManager):
pass
def invalidLogin():
pop = Popup(title='Invalid Login',
content=Label(text='Invalid username or password.'),
size_hint=(None, None), size=(400, 400))
pop.open()
def invalidForm():
pop = Popup(title='Invalid Form',
content=Label(text='Please fill in all inputs with valid information.'),
size_hint=(None, None), size=(400, 400))
pop.open()
kv = Builder.load_file("banyaknyakerja.kv")
sm = ScreenManager()
screens = [CreateAccountWindow(name="create"),MainWindow(name="main")]
for screen in screens:
sm.add_widget(screen)
sm.current = "create"
class MyMainApp(App):
def build(self):
return sm
if __name__ == "__main__":
MyMainApp().run()
KV 文件:
<WindowManager>:
CreateWindow:
id:create
name: "create"
MainWindow:
id: main
name: "main"
<CreateAccountWindow>:
panjang: panjang
lebar: lebar
tinggi: tinggi
FloatLayout:
cols:1
FloatLayout:
size: root.width, root.height/2
Label:
text: "serfbgokmfor"
size_hint: 0.8, 0.2
pos_hint: {"x":0.1, "top":1}
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8}
text: "Panjang: "
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8}
size_hint: 0.4, 0.12
id: panjang
multiline: False
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8-0.13}
text: "Lebar: "
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8-0.13}
size_hint: 0.4, 0.12
id: lebar
multiline: False
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8-0.13*2}
text: "Tinggi:"
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8-0.13*2}
size_hint: 0.4, 0.12
id: tinggi
multiline: False
font_size: (root.width**2 + root.height**2) / 14**4
Button:
pos_hint:{"x":0.3,"y":0.25}
size_hint: 0.4, 0.1
font_size: (root.width**2 + root.height**2) / 17**4
text: "Fefrkglt;mlfavsmdmcfr"
on_release:
root.manager.transition.direction = "left"
root.login()
Button:
pos_hint:{"x":0.2,"y":0.05}
size_hint: 0.6, 0.15
text: "Kira"
font_size: (root.width**2 + root.height**2) / 14**4
on_release:
root.manager.transition.direction = "left"
root.submit()
<MainWindow>:
panjang: panjang
lebar: lebar
tinggi: tinggi
luas1: luas1
luas2: luas2
cat: cat
FloatLayout:
Label:
id: panjang
pos_hint:{"x": 0.1, "top":0.9}
size_hint:0.8, 0.2
text: "Panjang: "
Label:
id: lebar
pos_hint:{"x": 0.1, "top":0.8}
size_hint:0.8, 0.2
text: "Lebar: "
Label:
id: tinggi
pos_hint:{"x": 0.1, "top":0.7}
size_hint:0.8, 0.2
text: "Tinggi:"
Label:
id: luas1
pos_hint:{"x": 0.1, "top":0.6}
size_hint:0.8, 0.2
text: "Luas:"
Label:
id: luas2
pos_hint:{"x": 0.1, "top":0.5}
size_hint:0.8, 0.2
text: "Luas:"
Label:
id: cat
pos_hint:{"x": 0.1, "top":0.4}
size_hint:0.8, 0.2
text: "Cat:"
Button:
pos_hint:{"x":0.2, "y": 0.1}
size_hint:0.6,0.2
text: "Semula"
on_release:
app.root.current = "create"
root.manager.transition.direction = "down"
(它告诉我显示最少的代码,但我真的不知道这里的问题)
我预期的输出不完整,并显示错误消息:
File "C:\Users\dekmeymey\.kivy\banyaknyekerja.py", line 60, in on_enter
self.volume() # calculate volume
File "C:\Users\dekmeymey\.kivy\banyaknyekerja.py", line 52, in volume
luas1 = float(self.manager.ids.create.panjang.text) * float(self.manager.ids.create.tinggi.text) / 10
File "kivy\properties.pyx", line 863, in kivy.properties.ObservableDict.__getattr__
AttributeError: 'super' object has no attribute '__getattr__'
(上面还有很多其他错误消息,但我认为这部分是最重要的。)
我希望它有点像这样:
Panjang: 23
Lebar: 11
Tinggi: 12
luas1: blabla
luas2: blabla
cat yang diperlukan: blabla
你明白了。
(我不知道如何插入图片,所以我这样做了。)
但我只得到这个:
Panjang:
Lebar:
Tinggi:
luas1:
luas2:
cat yang diperlukan:
我该如何解决这个问题?
【问题讨论】:
-
"(它告诉我显示最少的代码,但该死的我真的不知道这里的问题)"
-
您正在引用
self.manager.ids.create,我假设它试图引用<WindowManager>文件中<WindowManager>规则中提到的createid。但是,您没有使用WindowManager。您在sm = ScreenManager()中使用标准ScreenManager。 -
那么我应该将 WindowManager 更改为 ScreenManager 还是相反?编辑:我尝试更改它,它根本没有显示任何内容
-
不客气所以你的意思是我需要删除不必要的行?
-
@daunfifi123c456b 是的,尽可能多地删除。
标签: python kivy attributeerror getattr getattribute