By Erdo via Discussions on Python.org at 04Sep2022 01:55:
I made a module to manage the menus of an lcd display.
The number of options is quite large so I plan to use this file as a module to import into a file that integrates other issues.
That’s a good plan: write something once, use it elsewhere.
And my question goes this way. For example, when I select to log in to a page in the menu, what I do in the menu is put a variable that when entering that option becomes true.
So in the main file I have something like:
import menus
ShowMenu = menus.displayMenu(initial_menu)
ShowMenu.start()
while 1:
if ShowMenu.login == True:
makesomething
ShowMenu.login = False
if.... etc.
This is well? I mean, interact in this way with the other module? have a variable in the menus module that help me organize the main flow?
It looks ok. If you’re talking about ShowMenu.login
, that isn’t a
variable in the menus module. It is an attribute of the ShowMenu
object which you made here. (Well, I assume menus.displayMenu() returns
a new object - maybe it does not.) That’s a normal thing to do: have an
object representing some state.
There’s a bunch of random other comments about your code:
ShowMenu = menus.displayMenu(initial_menu)
I’m assuming ShowMenu
is some object, an instance of a class. This is
tritely true about all objects/values in Python, but what I mean here
is that you made some object representing your state, and returned it.
The common convention in Python is to use CamelCase names (like your
ShowMenu
name) for classes, and to use snake_case names such as
show_menu
for instances. So I’d rename StartMenu
to start_menu
.
The language itself does not dictate this, but the convention is nearly
universal in Python programming and adopting it will make your code
easier to understand.
while 1:
This is an infinite loop. Which is fine. We usually use the builtin
Python Booleans True
and False
for outright values like this, so
you’d more normally write:
while True:
Also, genuinely infinitely loops are rare. An interactive programme
normally runs until told to quit. So you might go:
done = False
while not done:
which will also run forever, but leaves you the option of setting
done=True
somewhere in response to something to quit the program by
exiting the loop.
if ShowMenu.login == True:
if-statements test truthiness: you can just write:
if ShowMenu.login:
here.
The final remark is that this looks like a “busy loop”: it polls
ShowMenu.login
every time, and if that’s not true does nothing. If
therest of the loop was also like that (poll various things) then when
all those things are off your loop spins flat out, while doing
nothing. Normally you’d have some kind of loop which responded to events
instead of spinning. This is so commmon that most GUI toolkits have a
function named something like mainloop()
which does just that, and
your setup involves making various widgets, then calling mainloop()
.
That sits there listening to the desktop events and responding as they
occur.
Cheers,
Cameron Simpson cs@cskk.id.au