kivy textinput 无法输入文字
Posted
技术标签:
【中文标题】kivy textinput 无法输入文字【英文标题】:kivy textinput can't enter text 【发布时间】:2017-08-24 10:52:51 【问题描述】:我目前正在编写一个使用 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()
我该如何解决这个问题?
【问题讨论】:
1.您可以在单击并使用按钮调用可以从该文本输入中提取文本的函数后尝试在文本输入中输入一些内容,以确保它至少可以正常工作。2 尝试更改光标的颜色 当我提取文本时,它始终是默认文本。不能改变它。我更改了光标的颜色,但是当我单击文本框时没有任何反应。所以光标不会像它应该的那样在文本框中闪烁 【参考方案1】:我自己解决了。我的错误是使用浮动布局。更好的方法是使用两个网格布局:
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()
【讨论】:
以上是关于kivy textinput 无法输入文字的主要内容,如果未能解决你的问题,请参考以下文章
(Kivy/Python)当用户点击输入时从 textinput 小部件存储文本