Main Menu

search

You are here

GUIzero Widget Programming

[last updated: 2025-02-21]
GUIzero home page
(link to:) Github: widget documentation
widget Colors
-----

      ... this page is very much a mess of in-process notes ...




            On This Page:
      • Introduction;
      • Widgets - General:
      • Creating Widgets:
      • Modifying a widget's Properties after it's created:


      ---------------------------------------------------------

      • Introduction:
        • A GUIzero/python GUI program starts with an App object. This is the first thing that is created.
          It is a container, and you put other objects into it. Specifically, you put Widgets into it.
        • A widget is an object that gets placed into a container.
        • But not all objects are containers. That is, you can only put a new widget into an object that is also a container.
          Objects that are containers (and therefore can contain other objects,) include:
              App, Window, and Box (?TitleBox?)
        • When you first start your program, the main window (App) is the only object you have. It is a container, so that's where your first widgets must be placed.
          You then proceed to add other widgets as desired to construct your GUI.
          These subsequent widgets can be placed in your original App object, or they can be put into any other container/object that you have placed in your App.
        • So in the end you have a set of nested objects, starting with the initial App, other objects in the App, other objects in those objects, other objects nested in those objects, etc.

        ---------------------------------------------------------

      • Widgets - General:
        • This task is complicated, because there are 14 widgets to choose from (by my count, not including App),
          each with its own set of parameters, properties, and methods.
          • When you decide you want to use a particular widget,
            you must know what are its parameters and properties and methods.
            • Parameters are those things (eg. sometimes: height, width, etc) that get defined when the widget/object is created.
            • Properties are those things (eg. sometimes: bg ie. background color) that get set After the widget is created.
            • Methods are ...
        • Important/basic widgets you'll surely use include:
          • Box:
          • PushButton:
          • Text:
        • Other widgets:

        ---------------------------------------------------------

      • Creating Widgets:
        • General syntax:
          [widgetVarName] = [widgetName] ( [masterName], [parameters], ... )
          • [widgetVarName] is your choice of variable name to attach to the widget so you can manipulate it later in your program.
          • [widgetName] is any one of the 14 or so defined widgets that you want to use.
          • [masterName] is the name of the container that will hold your new widget.
            This is the one required parameter for all widgets, except the initial App object.
          • [parameters] are any number of things you want to specify to define your widget.

        • Examples:
          • Create a box:
            boxName = (mainW) - this is minimum declaration (however height and width should also be specified)
            mainW is the name of the widget/container where the box will be placed. it must be either an App, a Window, or a Box.

        ---------------------------------------------------------

      • Modifying a widget's properties after it's created:
        • Parameters:
          These are the things that can be specified when you create the widget:
          • master - the name of the container, inside of which the box will be placed
          • layout - "auto" or "grid" - to specify how widgets that are placed inside this box will be arranged ***
          • grid - if the box's master container has layout "grid", ***
            then this parameter specifies where in the master widget's grid this box will be placed.
            Default is None, or you can specify a coordinate with ... = [x, y]
          • align - if the box's widget location has layout "auto" ***
            then you can set align = "top" or "bottom" or "left" or "right"
          • height and width - in pixels or "fill" to span the full dimension of its containing widget ***
          • border - 0 or False for no border, True for a border of 1, or an integer > 1 for a wider border ***
          • visible - True or False ***
          • enabled - True or False ***
        • Properties: (that are not also parameters)
          • bg -
          • children -
          • font -
          • text_size -
          • text_color -
          • tk -
        • Methods:

        ----------

      • PushButton:
        • Create a pushButton:
          PB1 = PushButton(mainW, command=doSomething)
          These are the minimum parameters that must be speficied: master = name of the container where the PB will be placed,
          and command is the name of the function that will be executed when you push the PB.
        • Parameters:
          These are the things that can be specified when you create the widget:
          • master - the name of the container, inside of which the PB will be placed
          • command - name of the function that will be executed
          • args - this is the comma-separated list of arguments you want to pass to the command function
            eg: PBx = PushButton(mainW, command=doThis, args= ...syntax?...
          • text -
          • image -
          • grid - if the PB's master container has layout "grid",
            then this parameter specifies where in the master widget's grid this PB will be placed.
            Default is None, or you can specify a coordinate with ... = [x, y]
          • height and width - in characters or "fill" to span the full dimension of its containing widget
          • padx and pady - pixels of spacing between edge of PB and any text placed in it
          • text - to be displayed
          • visible and enabled - True or False (default True)
        • Properties:
          These are like (and a few are identical to...) parameters used in the widget definition statement.
              They can be get:
                eg: print(PB1.bg)
              or set:
                eg: PB1.bg = "red"

        • Using tk.config:
          • I suspect these are tk widget properties...
          • PB1.tk.config(borderwidth = 6)
        • Methods:
          ----------

          • bg - background color
          • align
          • enabled and visible - True or False
          • font - a string name of the font style of the text on the button
          • grid - [x,y] coordinates of this widget in its container's grid
          • height and width - in characters or pixels (???) or "fill"
          • image - string of path to image
          • master
          • text - string to be displayed
          • text_color
          • text_size - in pixels
          • tk - tkinter.Button - The internal tkinter object, see Using tkinter
          • value - int - returns 1 when the button is pressed, else 0

        ----------

      • Text:
        ----------
      • TextBox:
        ----------

      • As said, widgets can get complicated. Here's my page of more details.

      -----------------------------------------------------

    • Layout:
      How/where will your widgets be placed?
      • An important concept to understand:
        • When you create a widget, you must put it inside a container.
          App, Window, and Box objects in your system can all act as containers (masters) to contain other widgets.
        • The first object you create is your mainWindow. It must be done with the App module:
            mainWindow = App()

      • Every subsequent object/widget you create will be placed inside some already existing container (App, Window, or Box).
          This is specified with the first parameter (the 'master' parameter) in your widget definition statement:
            widget2Box = Box(mainWindow)
      • The second object you define (we'll call this your first widget) must be inside your mainWindow, because that's the only container you have when you start.
        Now when you create your second widget, whatever it may be,
        you may have a choice of which container to put it into.
        If your first widget is a Box or a Window or an App, then it can act as a container for your second widget.
        In that case, when you define your second widget, you can choose whether to put it into your original mainWindow (App),
        or whether to put it into your first widget (assuming it's a Box, or a Window, or an App object).
          widget3Box = Box(mainWindow)
          -or- widget3Box = Box(widget2Box)
      • Exact position/placement inside the container where your new widget will be placed
        is determined by the "layout" and other properties that you specify when you create the containers.
      • The "layout" parameter can be either "auto" or "grid"
        • Layout is set when you define your object/widget/window. For example:
            mainWindow = App(layout="auto")
        • If you specify an "auto" layout (the easiest/simplest), for your object/container, then
          widgets placed in it will be centered and stacked vertically
          (unless it's a box and you've specified something different with the "align" parameter).

eof