J
John Dann
Trying to learn Python here, but getting tangled up with variable
scope across functions, modules etc and associated problems. Can
anyone advise please?
Learning project is a GUI-based (wxPython) Python program that needs
to access external data across a serial port.
The first thing that I need the program to do when it starts up is to
check that it can see the serial port, the first step of which is to
check that it can import the pySerial module, and display an
appropriate message in the statusbar of the main frame. (I know there
may be other ways of doing this but this is what I'm trying to do
currently and I'd like to understand how to make this approach work
robustly even if there are other options.)
So it seems that if I want to write to the frame's statusbar, I can't
do anything re serial import until the frame is initialising. And I
can't put any serial import code after the app.Mainloop() code line
because it won't run until the frame closes.
In other words, I seem to be stuck with importing the serial library
either in the __init__ function or - if implemented slightly
differently - in a separate function of the wxPython Frame class, ie a
code structure of:
-----------
# Can't put serial import code here because there's no GUI yet to send
messages to.
Class Frame(wx.Frame)
def __init__()
etc
#code to check and use serial port has to go below??
def CheckSerial() # eg triggered by appropriate menu click event
Try:
import serial
etc
ser = Serial.etc # set ser object as handle to serial port
def UseSerial():
# Code to perform serial IO
app=App(wx.App)
app.MainLoop()
# Can't put serial import code here because it won't execute until
frame closes?
-----------
But then I hit another problem. If I set the object ser to point to
the serial port in the CheckSerial function, then it's just got local
scope within the function and can't be seen from the UseSerial
function.
So I've got 2 questions. The main one is whether there's any way of
explicitly giving the ser object global scope in the code line:
ser = Serial.etc # set ser object as handle to serial port
And, second, does my overall description above reveal any serious
misunderstanding about how best to handle this sort of problem in
Python?
Apologies for the long post and hope that my description is clear
enough to make sense.
JGD
scope across functions, modules etc and associated problems. Can
anyone advise please?
Learning project is a GUI-based (wxPython) Python program that needs
to access external data across a serial port.
The first thing that I need the program to do when it starts up is to
check that it can see the serial port, the first step of which is to
check that it can import the pySerial module, and display an
appropriate message in the statusbar of the main frame. (I know there
may be other ways of doing this but this is what I'm trying to do
currently and I'd like to understand how to make this approach work
robustly even if there are other options.)
So it seems that if I want to write to the frame's statusbar, I can't
do anything re serial import until the frame is initialising. And I
can't put any serial import code after the app.Mainloop() code line
because it won't run until the frame closes.
In other words, I seem to be stuck with importing the serial library
either in the __init__ function or - if implemented slightly
differently - in a separate function of the wxPython Frame class, ie a
code structure of:
-----------
# Can't put serial import code here because there's no GUI yet to send
messages to.
Class Frame(wx.Frame)
def __init__()
etc
#code to check and use serial port has to go below??
def CheckSerial() # eg triggered by appropriate menu click event
Try:
import serial
etc
ser = Serial.etc # set ser object as handle to serial port
def UseSerial():
# Code to perform serial IO
app=App(wx.App)
app.MainLoop()
# Can't put serial import code here because it won't execute until
frame closes?
-----------
But then I hit another problem. If I set the object ser to point to
the serial port in the CheckSerial function, then it's just got local
scope within the function and can't be seen from the UseSerial
function.
So I've got 2 questions. The main one is whether there's any way of
explicitly giving the ser object global scope in the code line:
ser = Serial.etc # set ser object as handle to serial port
And, second, does my overall description above reveal any serious
misunderstanding about how best to handle this sort of problem in
Python?
Apologies for the long post and hope that my description is clear
enough to make sense.
JGD