我正在尝试从一个屏幕从文件选择器加载图像,并尝试在另一个屏幕上显示该图像的matplotlib图形 我使用了Kivy_matplotlib库 这是密码
import numpy as np
import matplotlib.widgets as widgets
import PIL
import matplotlib as mpl
from matplotlib import pyplot as plt
from PIL import Image
from kivy.app import App
from kivy.lang import Builder
from kivy_matplotlib import MatplotFigure, MatplotNavToolbar
from kivy.uix.screenmanager import ScreenManager, Screen
kv = """
<ScreenTwo>:
id:sc1
BoxLayout:
FileChooserListView:
id: filechooser
on_selection: my_widget.selected(filechooser.selection)
Button:
text: "Go to Screen 1"
on_press:
root.manager.transition.direction = 'right'
root.manager.current = 'screen_one'
<ScreenOne>
BoxLayout:
orientation: 'vertical'
Button:
text:"Choose File"
on_press:
root.manager.transition.direction = 'right'
root.manager.current = 'screen_two'
MatplotFigure:
id: figure_wgt
size_hint: 1, 0.9
MatplotNavToolbar:
id: navbar_wgt
size_hint: 1, 0.1
figure_widget: figure_wgt
"""
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
# The ScreenManager controls moving between screens
screen_manager = ScreenManager()
# Add the screens to the manager and then supply a name
# that is used to switch screens
screen_manager.add_widget(ScreenOne(name="screen_one"))
screen_manager.add_widget(ScreenTwo(name="screen_two"))
class testApp(App):
title = "Test Matplotlib"
def build(self):
# Matplotlib stuff, figure and plot
fig = mpl.figure.Figure(figsize=(2, 2))
def onselect(eclick, erelease):
if eclick.ydata>erelease.ydata:
eclick.ydata,erelease.ydata=erelease.ydata,eclick.ydata
if eclick.xdata>erelease.xdata:
eclick.xdata,erelease.xdata=erelease.xdata,eclick.xdata
ax.set_ylim(erelease.ydata,eclick.ydata)
ax.set_xlim(eclick.xdata,erelease.xdata)
fig.canvas.draw()
fig = plt.figure()
ax = fig.add_subplot(111)
filename="phase.jpg"
im = Image.open(filename)
arr = np.asarray(im)
plt_image=plt.imshow(arr)
rs=widgets.RectangleSelector(
ax, onselect, drawtype='box',
rectprops = dict(facecolor='red', edgecolor = 'black', alpha=0.5, fill=True))
#plt.show()
print(arr)
# Kivy stuff
root = Builder.load_string(kv)
figure_wgt = ScreenOne.ids['figure_wgt'] # MatplotFigure
figure_wgt.figure = fig
return screen_manager
#testApp().run()
sample_app = testApp()
sample_app.run()
当我得到以下信息时,我被卡住了 回溯
^{pr2}$需要帮助吗 提前谢谢
您需要重新排序代码执行流程。在添加小部件之前,必须在语言生成器中插入字符串。在
相关问题 更多 >
编程相关推荐