调整kivy中的按钮大小

Resizing the buttons in kivy

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 中绘制这些值?

您可以使用 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))

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 and KivyShowcase

编辑:1

糟糕!我忘了添加这部分。

how can i plot these values inside the 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)
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 绘制绘图和更改其大小和形状的选项。