Sulabh Tiwari
Sulabh Tiwari

Reputation: 307

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()    

This program is running fine however i am not able to figure out why the size and position of buttons are not changing?

dataset contains numeric value say say two dimension

1 2

3 4

5 6

7 8

how can i plot these values inside the GUI?

Upvotes: 3

Views: 12035

Answers (2)

Sulabh Tiwari
Sulabh Tiwari

Reputation: 307

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()

Here is the running code, however how can i use canvas to draw plots and option to change size and shape of it.

Upvotes: 0

kiok46
kiok46

Reputation: 1714

You can work with Float Layout

add this in your main.py file

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))

See here under layout section for more on layouts.

You might wanna checkout KivyCatalog and KivyShowcase.

EDIT: 1

Oops! I forgot to add this part.

how can i plot these values inside the GUI?

Use 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)

Upvotes: 5

Related Questions