Main Menu

search

You are here

guizero-importing def files

[last updated: 2025-10-30]
GUIzero parameter programming
GUIzero Widget programming
GUIzero - importing def files
Python: Programming
(link to:) Github: GUIzero Help
(link to:) lawsie - guizero reference

installing custom fonts
-----------------------------------------

      ... edit in process ...

  • Why would you want to do this?
    So you've built a GUI with guizero,
    but maybe it's just getting too large and difficult to maintain,
    or perhaps you have code in it that you want to use in other programs,
    but the bottom line is that you want to remove some of the code from your program
    and put it into a separate file, then import it back so you can still use it in your main program.
    -------------------------------

  • The easy case:
    If the code you want to put into a separate file is just eg. variables and their values, or perhaps calculations,
    then just put them into a separate file with a .py extension, then import them back with:
      from [filename] import *
      ([filename] does NOT include the .py extension)

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

  • The more difficult case - you want to separate out functions that create widgets:

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

  • The even more difficult case:
    You want to have a def in a separate file that creates a widget (call it secondBox),
    then you want a def in a different file that creates another widget (thirdBox) inside secondBox.
    • First my disclaimer:
      I don't really understand this process very deeply. In trying to make it work for my project,
      I did lots of surfing and found lots of tips, but nothing that exactly fit my use case,
      so I did the usual hacking around and trying things and the following is what finally worked.

    • The basic setup:
      You have 2 defs to create widgets, and they are in separate standalone .py files.
      You import the standalone files, and call the defs from your main program.


      So for example:

        In this example, I'm going to draw 3 nested boxes inside mainW.
        The first one (firstBox) will be created in the main program, and drawn directly into mainW.
        The second one (secondBox) will be created inside firstBox by an imported def function.
        The third one (thirdBox) will be created inside secondBox by another imported def function.
      • Main program: myMain.py
          from guizero import *
          from createWidget_1 import *
          mainW = App(title=mainW_Title, width=800, height=800)
          # create firstBox:
          firstBox = Box(mainW, height=400, width=400, border=1)
          # execute imported function:
      • Standalone .py file: createWidget_1.py
          from guizero import *
          def drawFirstBoxWht(mainW):
                boxWht = Box(mainW, height=20, width=100, border=1)

        # --------------------------

        mainW.display()

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


.


eof