import numpy as np
import kivy
kivy.require('1.0.6') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
class myLayout(BoxLayout):
def __init__(self, **kwargs):
super(myLayout, self).__init__(**kwargs)
btn1 = Button(text = "click 1", background_color=[0,0,1,0],pos=(200, 100))
btn1.bind(on_press=self.clk1)
btn2 = Button(text = "click 2", pos=(200, 100))
btn2.bind(on_press=self.clk)
btn3 = Button(text = "click 3", pos=(50, 100))
btn3.bind(on_press=self.clk)
self.add_widget(btn1)
self.add_widget(btn2)
self.add_widget(btn3)
def clk(self, obj):
print("Hello WOrld")
def clk1(self, obj):
dataset = np.genfromtxt(fname='data.txt',skip_header=1)
print dataset
class NameApp(App):
def build(self):
mL = myLayout()
return mL
if __name__ == '__main__':
NameApp().run()
这个程序运行正常但是我无法弄清楚为什么按钮的大小和位置没有变化?
数据集包含数值,例如说二维
1 2
3 4
5 6
7 8
如何在GUI中绘制这些值?
答案 0 :(得分:2)
您可以使用Float Layout
在main.py文件中添加
from kivy.uix.floatlayout import FloatLayout
...
btn1 = Button(text = "click 1",pos=(200, 200),size_hint = (.1,.1))
btn2 = Button(text = "click 2", pos=(200, 100),size_hint = (.1,.1))
btn3 = Button(text = "click 3", pos=(50, 100),size_hint = (.1,.1))
OR
btn1 = Button(text = "click 1",pos_hint= {'x': .1,'top': .2},size_hint = (.1,.1))
btn2 = Button(text = "click 2", pos_hint= {'x': .4,'top': .2},size_hint = (.1,.1))
btn3 = Button(text = "click 3", pos_hint= {'x': .66,'top': .2},size_hint = (.1,.1))
有关布局的详情,请参阅布局部分下的here。
您可能想要结帐KivyCatalog和KivyShowcase。
编辑:1
糟糕!我忘了添加这部分。
如何在GUI中绘制这些值?
使用Label
self.lbl = Label(pos_hint= {'x': .73,'top': .6},size_hint = (.1,.1))
self.add_widget(self.lbl)
def clk1(self, obj):
....
self.lbl.text = str(dataset)
答案 1 :(得分:0)
import kivy
kivy.require('1.0.6') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.garden.graph import Graph, MeshLinePlot
import math
from Tkinter import Tk
from tkFileDialog import askopenfilename
import os
class myLayout(FloatLayout):
def __init__(self, **kwargs):
super(myLayout, self).__init__(**kwargs)
btn1 = Button(text = "click 1",pos_hint= {'x': .1,'top': .2},size_hint = (.1,.1))
btn2 = Button(text = "click 2", pos_hint= {'x': .4,'top': .2},size_hint = (.1,.1))
btn3 = Button(text = "click 3", pos_hint= {'x': .66,'top': .2},size_hint = (.1,.1))
btn1.bind(on_press=self.clk1)
btn2.bind(on_press=self.clk2)
btn3.bind(on_press=self.clk3)
self.add_widget(btn1)
self.add_widget(btn2)
self.add_widget(btn3)
def clk1(self, obj):
print("Hello World")
def clk2(self, object):
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
def clk3(self, object):
graph = Graph(xlabel='X', ylabel='Y', x_ticks_minor=5,x_ticks_major=25, y_ticks_major=1,y_grid_label=True, x_grid_label=True, padding=5,
x_grid=True, y_grid=True, xmin=-0, xmax=100, ymin=-1, ymax=1)
plot = MeshLinePlot(color=[1, 0, 0, 1])
plot.points = [(x, math.sin(x / 10.)) for x in range(0, 101)]
graph.add_plot(plot)
self.add_widget(graph)
class NameApp(App):
def build(self):
mL = myLayout()
return mL
if __name__ == '__main__':
NameApp().run()
以下是正在运行的代码,但是如何使用canvas绘制绘图和选项来更改它的大小和形状。