Main Menu

search

You are here

GUIzero home page

[last updated: 2025-02-21]
GUIzero parameter programming
GUIzero Widget programming
Python: Programming
(link to:) Github: GUIzero Help



      On This Page:
  • Installation/Run
  • Basic Program Protocol for GUIzero/Python GUI programs
  • Widgets
  • ...
  • Links/Refs


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

Great tutorial: https://dev.to/jr_shittu/getting-started-with-graphical-user-interfaces-...
other good tips: https://lawsie.github.io/guizero/usingtk/
https://github.com/rdbende/Azure-ttk-theme
https://github.com/TomSchimansky/CustomTkinter
also research: tkbootstrap and PyQt
-----

  • A python3 library for creating easy GUI's.
    Can be used along with Tkinter.

  • Installation/Run:
    • Installation:
      • Tkinter is (these days) supposedly bundled with rPiOS, so no need to install it separately.
        But if you need to install it:
          $ sudo apt install python3-guizero
          (I have not personally tested this...)
      • Install guizero:
          This was troublesome on my rPi's.
          I hacked around and managed to get it done, but as usual have no clue what actually made it work,
          but here are the things I did:
            $ ... update
            $ ... upgrade
            $ ... dist-upgrade
            loaded new rPiOS (probably what worked...)

          Actual install command, from terminal:

            $ sudo apt install python3-guizero
      • Install gpiozero:
        This package is required for easy programming of physical I/O on the rPi.
        It is not required to run guizero, but it IS required if you're going to interface with any of the rPi gpio pins.
        It is bundled with the current rPiOS, but if you need to install it manually...
        (link to:) gpiozero docs
        (link to:) install gpiozero
          $ sudo apt install python3-gpiozero

    • Run:
      • Create testProg.py (or whatever filename you want), and write your GUI code into the python file.
      • Execute it from CLI with:
        $ cd [location of python program]
        $ sudo python3 testProg.py

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

  • Basic Programming Protocol for GUIzero Python GUI program:
    • First line is import line(s):
        from guizero import *

      This is a simple minimum. If you want to preserve ram, you might want to only import the specific guizero modules that you'll be using, eg:

        from guizero import App, Box, PushButton
    • There may be programs that require additional imports, eg:
        from tkinter import *

      I saw one forum note saying if you use tkinter, the import line must be first, ie. above/before the import guizereo line.

    • Next lines in your .py program will be def lines, ie. function definitions
      If your GUI is going to do anything, interact with the user, then you need a function to tell it what to do.
      You define funtions with:
        def funcName():
          [program statements]
          eg:
          print("Hello World")
          varName1 = "sayWhat"
          varName2 = 47

      The "funcName" is the variable you assign to refer to the function.
      As always in python, proper indentation is essential.
      Program statements in a def must be indented 4 spaces.
      The end of the def is signaled by a return to no indent
      -------------

    • Next you create your main window using the App module:
      • mainWindow = App()
        "mainWindow" is the variable name you assign. App is the guizero module that you call to create the window.
          A lot of online forums and tutorials use "app" as the variable name for your main window, eg: app = App()
          Do not confuse this variable name ("app") with the module name "App".
      • Like all guizero objects, an App object has its own set of:
        • parameters that can be set at time of creation.
        • properties that can be get and set after creation.
          Some properties are also parameters.
        • methods or operations/functions that can be performed on them.
      • When you create an App object (or any guizero object), you can define your desired parameters by placing them inside the "( )".
        Multiple parameters are separated by commas.
        There are no minimum/required parameters for an App object.
          For example:
          mainWindow = App(bg="blue", title="myMainWindow", width=500, height=500)
      • Documentation for the App object is here: https://lawsie.github.io/guizero/app/

      • Parameters: (*** denotes they are also properties)
        • bg = background color (default none) ***
        • height & width in pixels (default 500) ***
        • layout - either "auto" or "grid" (including quotes) ***
        • title - text displayed at the top of the window ***
        • visible - True or False ***
      • Properties: (that are not also parameters)
        • children - a list of widgets in this container
        • font - string fontName for widgets contained
        • full_screen - True or False (default)
        • enabled - True or False
        • image - string pathName to image
        • text_size -
        • text_color -
        • tk - the internal tkinter object
        • when_closed - function name to call if the App is closed
      • Methods:
          see: Documentation link above

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

  • WIDGETS
    -----------------------------------------------------
  • Finally, the last line of your .py program must be:
      mainWindow.display()

    This is the line that executes/renders your GUI creation onto your screen.
    This starts an infinite update loop that continuously waits for (and responds to) events,
    like mouse movements, clicks, or text typed into text boxes, etc.


-----------------------------------------------------
from guizero import App, Text
from tkinter import Label

a = App()
text_1 = Text(a, text="text 1")
label = Label(a.tk, text="label")
label.pack()
text_2 = Text(a, text="text 2")

a.display()
-----------------------------------------------------

  • Links/Refs:

    Installation: (link to:) lawsie.github

    -----

  • to investigate:

    from:
    https://github.com/lawsie/guizero/issues/434

    from guizero import App, Text
    from tkinter import Label, PhotoImage

    app = App("I have a background image")

    # Some raw tkinter to create the bg image
    img = PhotoImage(file="bgimage.png")
    label = Label(app.tk,image=img)
    label.place(x=0, y=0)

    # Text on top, but it will have a background
    some_text = Text(app, "This is some text")

    app.display()
    ----------------------------------------------------------------

    from:
    https://github.com/lawsie/guizero/issues/399

    You can access the underlaying TK object and set its options, in this case the borderwidth (or bd) property

    from guizero import App, PushButton

    app = App("A picture button")

    picture_button = PushButton(app, image="guizero.gif")
    picture_button.tk.config(borderwidth = 0)

    app.display()
    ----------------------------------------------------------------
    im = Image.open(pathToImage)
    ph = ImageTk.PhotoImage(im)

    label = Label(window, image=ph)
    label.image=ph #need to keep the reference of your image to avoid garbage collection
    ----------------------------------------------------------------

    .

    .

    .

    eof