我正在编写一个代码,用matplotlib绘制直方图。我用kivy来显示。当我运行我的代码时,我能够看到情节,所以没有问题。我想通过使用文本框和更新按钮手动输入垃圾箱。如果我用python输入文本(如默认值:50),如果我点击按钮,情节将自动更新,但当我想点击文本框输入文字时,没有任何反应。我没有在文本框中看到我的光标。
我的代码:
import matplotlib
matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
from kivy.app import App
import csv
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvas, NavigationToolbar2Kivy
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from mpldatacursor import datacursor
from kivy.uix.floatlayout import FloatLayout
import matplotlib.pyplot as plt
with open("c:/Users/test.csv", 'rb') as f:
reader = csv.reader(f, delimiter=';')
data = []
for column in reader:
data.append(column[1])
results = data
results = [int(i) for i in results]
bins = 200
plt.hist(results, bins=bins, color=[0,0,1])
fig = plt.gcf()
ax = fig.add_subplot(111)
ax.hist(results, bins=200, edgecolor=[0,0,0,1], alpha = 0.8, color= 'b')
datacursor(ax)
canvas = fig.canvas
def callback(bins):
global fig, results
plt.clf()
bins = int(bins)
plt.hist(results,bins=bins, color=[0,0,1])
ax = fig.add_subplot(111)
ax.hist(results, bins=bins, edgecolor=[0, 0, 0, 1], alpha=0.8, color='b')
datacursor(ax)
canvas.draw()
class HistogramApp(App):
def build(self):
f = FloatLayout(orientation='vertical')
g = GridLayout(size_hint=(1,0.05), cols=2)
self.btn1 = Button(size_hint=(0.5,0.1), pos_hint={'x':0, 'y':0}, text="Update")
self.textbox = TextInput(text="50", size_hint=(0.5,0.1), pos_hint={'x':0.5, 'y':0})
self.btn1.bind(on_press=lambda x: callback(self.textbox.text))
nav1 = NavigationToolbar2Kivy(canvas)
f.add_widget(canvas)
f.add_widget(nav1.actionbar)
f.add_widget(g)
g.add_widget(self.btn1)
g.add_widget(self.textbox)
return f
if __name__ == '__main__':
HistogramApp().run()
我该如何解决这个问题?
答案 0 :(得分:1)
我自己解决了。我的错误是使用了floatlayout。更好的方法是使用两个网格布局:
import matplotlib
matplotlib.use('module://kivy.garden.matplotlib.backend_kivy')
from kivy.app import App
import csv
from kivy.garden.matplotlib.backend_kivyagg import FigureCanvas, NavigationToolbar2Kivy
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from mpldatacursor import datacursor
from kivy.uix.floatlayout import FloatLayout
import matplotlib.pyplot as plt
with open("c:/Users/test.csv", 'rb') as f:
reader = csv.reader(f, delimiter=';')
data = []
for column in reader:
data.append(column[1])
results = data
results = [int(i) for i in results]
bins = 200
plt.hist(results, bins=bins, color=[0,0,1])
fig = plt.gcf()
ax = fig.add_subplot(111)
ax.hist(results, bins=200, edgecolor=[0,0,0,1], alpha = 0.8, color= 'b')
datacursor(ax)
canvas = fig.canvas
def callback(bins):
global fig, results
plt.clf()
bins = int(bins)
plt.hist(results,bins=bins, color=[0,0,1])
ax = fig.add_subplot(111)
ax.hist(results, bins=bins, edgecolor=[0, 0, 0, 1], alpha=0.8, color='b')
datacursor(ax)
canvas.draw()
class HistogramApp(App):
def build(self):
g1 = GridLayout(orientation="horizontal", rows=3)
g2 = GridLayout(orientation='horizontal', cols=2, size_hint=(0.05, 0.05))
self.btn1 = Button(size_hint=(0.5, 0.1), pos_hint={'x': 0, 'y': 0}, text="Update")
self.tekstvak = TextInput(text="50", size_hint=(0.5, 0.1), pos_hint={'x': 0.5, 'y': 0},cursor_color=[0, 0, 0, 1])
self.btn1.bind(on_press=lambda x: callback(self.tekstvak.text))
nav1 = NavigationToolbar2Kivy(canvas)
g1.add_widget(nav1.actionbar)
g1.add_widget(canvas)
g2.add_widget(self.tekstvak)
g2.add_widget(self.btn1)
g1.add_widget(g2)
return g1
if __name__ == '__main__':
HistogramApp().run()