Kivy vs Tkinter - First Try

From OpenCircuits
Jump to navigation Jump to search

What

I am trying to implement an older window of mine from Python Smart Terminal, done in Tkinter over again in Kivy. This is my first bit of code to show some progress, I am trying to do it without any Kv coding. Kivy - First Impressions

Tkinter

Here is the SmartTerminal window in Tkinter.

Screen shot.png

Kivi ( first try )

And here is the first Kivy windows that shows just a bit of progress. Not much, but the basic outline of the Tkinter code. One of the most useful references was Mouse vs Python

Gui 1a.png

Python Code

Less than great, but something


# -*- coding: utf-8 -*-

import kivy
import random

from kivy.app            import App

from kivy.uix.boxlayout  import BoxLayout
from kivy.uix.gridlayout import GridLayout

from kivy.uix.button     import Button
from kivy.uix.textinput  import TextInput
from kivy.uix.label      import Label
from kivy.uix.dropdown   import DropDown

# color declarations, see doc in ??
red     = [1,0,0,1]
green   = [0,1,0,1]
blue    = [0,0,1,1]
purple  = [1,0,1,1]


########################################################################
class GUI(App):
    """
    My first non trivial ( not by much example )
    """
    #----------------------------------------------------------------------
    def build(self):  # seems to be called automagically lets treat like init for now
        """
        Build all stuff here, will call frames after Tkinter, but of course they are really layouts
        """
        root          = BoxLayout( padding=10, orientation="vertical" )

        a_frame = self.__make_parm_frame__()
        root.add_widget( a_frame    )

        a_frame = self.__make_button_frame__()
        root.add_widget( a_frame    )

        a_frame = self.__make_send_frame__()
        root.add_widget( a_frame )

        a_frame = self.__make_rec_frame__()
        root.add_widget( a_frame )

        return  root

    # ------ ------------
    def __make_parm_frame__( self  ):

        max_cols     = 3

        a_layout = BoxLayout( padding=10 )   # default horizontal

        for i in range( max_cols ):
            lbl      = Label(text='Parameter')
            a_layout.add_widget( lbl    )

        return a_layout

    # ------ ------------
    def __make_button_frame__( self  ):

        colors       = [red, green, blue, purple]
        max_cols     = 3

        a_layout = BoxLayout( padding=10 )

        for i in range( max_cols ):
            btn = Button(text="Button #%s" % (i+1),
                         background_color=random.choice(colors)
                         )
            a_layout.add_widget( btn    )

        return a_layout

    # ------ ------------
    def __make_send_frame__( self  ):

        colors       = [red, green, blue, purple]
        max_cols     = 3

        a_layout = BoxLayout( padding=10 )

        for i in range( max_cols ):
            btn = Button(text="Button #%s" % (i+1),
                         background_color=random.choice(colors)
                         )
            a_layout.add_widget( btn    )

            txt_in   = TextInput(  )

            a_layout.add_widget( txt_in )

        return a_layout

    # ------------------------------------------
    def __make_rec_frame__( self, ):

        a_layout = BoxLayout( padding=10 )

        txt_in   = TextInput(  )

        a_layout.add_widget( txt_in )
        return a_layout

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = GUI()
    app.run()