你好,我试图从我的 main.py 中的导航布局下的屏幕中调用一个函数,但它显示了一个属性错误。
虽然代码正在运行,但在行屏幕中按下“绘图”按钮时出现错误。
错误是:AttributeError: 'Screen' 对象没有属性 'get_data'
Traceback (most recent call last):
File "main.py", line 162, in <module>
Graphs().run()
File "/home/saurabh/.local/lib/python3.7/site-packages/kivy/app.py", line 950, in run
runTouchApp()
File "/home/saurabh/.local/lib/python3.7/site-packages/kivy/base.py", line 582, in runTouchApp
EventLoop.mainloop()
File "/home/saurabh/.local/lib/python3.7/site-packages/kivy/base.py", line 347, in mainloop
self.idle()
File "/home/saurabh/.local/lib/python3.7/site-packages/kivy/base.py", line 391, in idle
self.dispatch_input()
File "/home/saurabh/.local/lib/python3.7/site-packages/kivy/base.py", line 342, in dispatch_input
post_dispatch_input(*pop(0))
File "/home/saurabh/.local/lib/python3.7/site-packages/kivy/base.py", line 308, in post_dispatch_input
wid.dispatch('on_touch_up', me)
File "kivy/_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
File "/home/saurabh/Desktop/BHUVNESH/Programming/graphs/environments/my_env/lib/python3.7/site-packages/kivymd/uix/behaviors/ripplebehavior.py", line 245, in on_touch_up
return super().on_touch_up(touch)
File "/home/saurabh/Desktop/BHUVNESH/Programming/graphs/environments/my_env/lib/python3.7/site-packages/kivymd/uix/button.py", line 1083, in on_touch_up
return super().on_touch_up(touch)
File "/home/saurabh/Desktop/BHUVNESH/Programming/graphs/environments/my_env/lib/python3.7/site-packages/kivymd/uix/button.py", line 969, in on_touch_up
return super().on_touch_up(touch)
File "/home/saurabh/.local/lib/python3.7/site-packages/kivy/uix/behaviors/button.py", line 179, in on_touch_up
self.dispatch('on_release')
File "kivy/_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
File "kivy/_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
File "kivy/_event.pyx", line 1132, in kivy._event.EventObservers._dispatch
File "/home/saurabh/.local/lib/python3.7/site-packages/kivy/lang/builder.py", line 57, in custom_callback
exec(__kvlang__.co_value, idmap)
File "<string>", line 214, in <module>
File "kivy/weakproxy.pyx", line 32, in kivy.weakproxy.WeakProxy.__getattr__
AttributeError: 'Screen' object has no attribute 'get_data'
我对此错误进行了一些研究,并尝试了其他调用方法,但找不到解决方案。请帮忙。
python 版本:v3.7.8 kivy 版本:v2.0.0 kivymd:v0.104.1
from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
from kivymd.uix.button import MDRaisedButton, MDTextButton,MDFillRoundFlatButton
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager,FallOutTransition
from kivymd.uix.list import MDList, OneLineListItem,OneLineIconListItem
from kivy.uix.scrollview import ScrollView
from kivy.lang import Builder
from navigation import screen_helper
from kivy.uix.image import Image
from kivymd.uix.textfield import MDTextField
from kivymd.uix.button import MDRectangleFlatButton
from kivy.uix.textinput import TextInput
from kivy.core.window import Window
import numpy as np
import matplotlib.pyplot as plt
from kivy.uix.boxlayout import BoxLayout
xpoints, ypoints = list(), list()
class WelcomeScreen(Screen):
pass
class ListScreen(Screen):
pass
class LineScreen(Screen):
def get_data(self,root):
t = self.ids.n.text
x1 = self.ids.x_label.text
y1 = self.ids.y_label.text
e1 = (self.ids.A.text)
e2 = (self.ids.B.text)
e3 = (self.ids.C.text)
e4 = (self.ids.D.text)
e5 = (self.ids.E.text)
e6 = (self.ids.F.text)
e7 = (self.ids.G.text)
e8 = (self.ids.H.text)
e9 = (self.ids.I.text)
e10 = (self.ids.J.text)
xpoints=[e1,e2,e3,e4,e5]
ypoints=[e6,e7,e8,e9,e10]
xpoints.sort()
ypoints.sort()
plt.plot(xpoints, ypoints, marker='o')
plt.title(t)
plt.xlabel(x1)
plt.ylabel(y1)
plt.draw()
plt.savefig("test.png")
plt.show()
class BarScreen(Screen):
pass
class ScatterScreen(Screen):
pass
class PieScreen(Screen):
pass
class SineScreen(Screen):
pass
class CosineScreen(Screen):
pass
class ContentNavigationDrawer(BoxLayout):
pass
sm = ScreenManager(transition=FallOutTransition())
sm.add_widget(WelcomeScreen(name='welcome'))
sm.add_widget(ListScreen(name='list'))
sm.add_widget(LineScreen(name='line'))
sm.add_widget(BarScreen(name='bar'))
sm.add_widget(ScatterScreen(name='scatter'))
sm.add_widget(PieScreen(name='pie'))
sm.add_widget(SineScreen(name='sine'))
sm.add_widget(CosineScreen(name='cosine'))
class Graphs(MDApp):
def build(self):
screen = Builder.load_string(screen_helper)
return screen
Graphs().run()
screen_helper = """
Screen:
NavigationLayout:
ScreenManager:
id:screen_manager
WelcomeScreen:
name:'welcome'
Image:
source:"g.png"
allow_stretch:True
keep_ratio:False
MDLabel:
text:'Hello!'
font_size:54
halign:'center'
theme_text_color:'Primary'
MDFillRoundFlatButton:
text:'CONTINUE'
pos_hint: {'center_x':0.8,'center_y':0.1}
elevation_normal: 12
on_press:screen_manager.current='home'
ListScreen:
name: 'list'
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Graphs'
left_action_items: [["menu",lambda x: navigation.set_state('open')]]
elevation: 10
ScrollView:
MDList:
OneLineIconListItem:
text:'Line Graph'
size:
on_press:screen_manager.current='line'
IconLeftWidget:
icon: 'lineicon.png'
OneLineIconListItem:
text:'Bar Graph'
on_press:screen_manager.current='bar'
IconLeftWidget:
icon: 'baricon.png'
OneLineIconListItem:
text:'Scatter Graph'
on_press:screen_manager.current='scatter'
IconLeftWidget:
icon: 'scattericon.png'
OneLineIconListItem:
text:'Pie Chart'
on_press:screen_manager.current='pie'
IconLeftWidget:
icon: 'pieicon.png'
OneLineIconListItem:
text:'Sine Wave'
on_press:screen_manager.current='sine'
IconLeftWidget:
icon: 'sineicon.png'
OneLineIconListItem:
text:'Cosine Wave'
on_press:screen_manager.current='cosine'
IconLeftWidget:
icon: 'cosicon.png'
MDRaisedButton:
text:'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press:screen_manager.current='home'
LineScreen:
name:'line'
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Line Graph'
left_action_items: [["menu",lambda x: navigation.set_state('open')]]
elevation: 10
Image:
source:"line.png"
allow_stretch:True
keep_ratio:False
MDTextField:
id:n
hint_text:"Enter Title of Graph"
mode: "rectangle"
helper_text_mode:"on_error"
required:True
pos_hint:{'center_x':0.5,'center_y':0.81}
size_hint_x:.4
width:150
MDTextField:
id:x_label
hint_text:"Enter X Label"
mode: "rectangle"
helper_text_mode:"on_error"
required:True
pos_hint:{'center_x':0.5,'center_y':0.71}
size_hint_x:.4
width:150
MDTextField:
id:y_label
hint_text:"Enter Y Label"
mode: "rectangle"
helper_text_mode:"on_error"
required:True
pos_hint:{'center_x':0.5,'center_y':0.61}
size_hint_x:.4
width:150
MDTextField:
id:A
input_filter:'int'
hint_text:"X1"
pos_hint:{'center_x':0.3,'center_y':0.52}
size_hint_x:None
width:30
MDTextField:
id:B
input_filter:'int'
hint_text:"X2"
pos_hint:{'center_x':0.3,'center_y':0.44}
size_hint_x:None
width:30
MDTextField:
id:C
input_filter:'int'
hint_text:"X3"
pos_hint:{'center_x':0.3,'center_y':0.36}
size_hint_x:None
width:30
MDTextField:
id:D
input_filter:'int'
hint_text:"X4"
pos_hint:{'center_x':0.3,'center_y':0.28}
size_hint_x:None
width:30
MDTextField:
id:E
input_filter:'int'
hint_text:"X5"
pos_hint:{'center_x':0.3,'center_y':0.2}
size_hint_x:None
width:30
MDTextField:
id:F
input_filter:'int'
hint_text:"Y1"
pos_hint:{'center_x':0.7,'center_y':0.52}
size_hint_x:None
width:30
MDTextField:
id:G
input_filter:'int'
hint_text:"Y2"
pos_hint:{'center_x':0.7,'center_y':0.44}
size_hint_x:None
width:30
MDTextField:
id:H
input_filter:'int'
hint_text:"Y3"
pos_hint:{'center_x':0.7,'center_y':0.36}
size_hint_x:None
width:30
MDTextField:
id:I
input_filter:'int'
hint_text:"Y4"
pos_hint:{'center_x':0.7,'center_y':0.28}
size_hint_x:None
width:30
MDTextField:
id:J
input_filter:'int'
hint_text:"Y5"
pos_hint:{'center_x':0.7,'center_y':0.2}
size_hint_x:None
width:30
MDRaisedButton:
text:'Back'
pos_hint: {'center_x':0.2,'center_y':0.1}
on_press:screen_manager.current='list'
MDRaisedButton:
text:"Plot "
pos_hint:{'center_x':0.8,'center_y':0.1}
on_press:root.get_data(root)
BarScreen:
name:'bar'
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Bar Graph'
left_action_items: [["menu",lambda x: navigation.set_state('open')]]
elevation: 10
Image:
source:"bar.png"
allow_stretch:True
keep_ratio:False
MDRaisedButton:
text:'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press:screen_manager.current='list'
ScatterScreen:
name:'scatter'
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Scatter Graph'
left_action_items: [["menu",lambda x: navigation.set_state('open')]]
elevation: 10
Image:
source:"scatter.png"
allow_stretch:True
keep_ratio:False
MDRaisedButton:
text:'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press:screen_manager.current='list'
PieScreen:
name:'pie'
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Pie Chart'
left_action_items: [["menu",lambda x: navigation.set_state('open')]]
elevation: 10
Image:
source:"pie.png"
allow_stretch:True
keep_ratio:False
MDRaisedButton:
text:'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press:screen_manager.current='list'
SineScreen:
name:'sine'
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Sine Wave'
left_action_items: [["menu",lambda x: navigation.set_state('open')]]
elevation: 10
Image:
source:"sine.png"
allow_stretch:True
keep_ratio:True
MDRaisedButton:
text:'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press:screen_manager.current='list'
CosineScreen:
name:'cosine'
MDBoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Cosine Wave'
left_action_items: [["menu",lambda x: navigation.set_state('open')]]
elevation: 10
Image:
source:"cos.png"
allow_stretch:True
keep_ratio:True
MDRaisedButton:
text:'Back'
pos_hint: {'center_x':0.5,'center_y':0.1}
on_press:screen_manager.current='list'
Screen:
name: 'home'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'Home'
left_action_items: [["menu",lambda x: navigation.set_state('open')]]
elevation: 10
Image:
source:"home1.png"
allow_stretch:True
keep_ratio:False
Screen:
name: 'about'
BoxLayout:
orientation: 'vertical'
MDToolbar:
title: 'About'
left_action_items: [["menu",lambda x: navigation.set_state('open')]]
opening_time: 0.05
elevation: 10
Image:
source:"home1.png"
allow_stretch:True
keep_ratio:False
MDNavigationDrawer:
id:navigation
ContentNavigationDrawer:
BoxLayout:
orientation: 'vertical'
padding: '3dp'
Image:
source: 'goku.jpg'
MDLabel:
text: 'Basic Graph Plotter'
font_style: 'H5'
size_hint_y: None
height : self.texture_size[1]
ScrollView:
MDList:
OneLineIconListItem:
text: 'Home'
on_press:
navigation.set_state('close')
closing_time: 0.05
screen_manager.current='home'
IconLeftWidget:
icon: 'home.png'
OneLineIconListItem:
text: 'Graphs'
on_press:
navigation.set_state('close')
closing_time: 0.05
screen_manager.current='list'
IconLeftWidget:
icon: 'graphs.png'
OneLineIconListItem:
text: 'About'
on_press:
navigation.set_state('close')
closing_time: 0.05
screen_manager.current='about'
IconLeftWidget:
icon: 'about.png'
"""