screen keyboard

345 views
Skip to first unread message

Ping

unread,
Mar 24, 2011, 6:06:20 AM3/24/11
to wxPython-users
Hey,guys!
Could anybody tell me how can use Python + wxpython write an on-
screen keyboard?

Tim Roberts

unread,
Mar 24, 2011, 1:03:16 PM3/24/11
to wxpytho...@googlegroups.com

Well, there's no magic here. I would probably do it by brute force,
creating a window with bunch of buttons, and connect the button handlers
to my keystroke events.

Some of the operating systems already have onscreen keyboards when you
use their touch extensions. What system are you writing for, and what
is the use case?

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Hugues JEAN-BAPTISTE

unread,
Mar 25, 2011, 4:02:59 AM3/25/11
to wxpytho...@googlegroups.com
Le 24/03/2011 18:03, Tim Roberts a �crit :

> Ping wrote:
>> Hey,guys!
>> Could anybody tell me how can use Python + wxpython write an on-
>> screen keyboard?
> Well, there's no magic here. I would probably do it by brute force,
> creating a window with bunch of buttons, and connect the button handlers
> to my keystroke events.
>
> Some of the operating systems already have onscreen keyboards when you
> use their touch extensions. What system are you writing for, and what
> is the use case?
>
Hi,

I am going to write a French keyboard too for Linux, because the
keyboard on Gnome does not work very well !...

--

Hugues JEAN-BAPTISTE (h...@agorinfo.fr)
AGORINFO S.A.S. (http://www.agorinfo.fr)

Ping

unread,
Mar 28, 2011, 11:26:34 PM3/28/11
to wxPython-users
> Tim Roberts, t...@probo.com
> Providenza & Boekelheide, Inc.



Thanks for your response, Tim.

We are a bunch of college students trying to develop a cross-platform
application which may assist the the ALS (Amyotrophic Lateral
Sclerosis) patients in using computers. The application include the
screen mouse and keyboard. As we are very new to Python and wxPython,
lots of things are there for us to learn.


Ping

unread,
Mar 28, 2011, 11:23:52 PM3/28/11
to wxPython-users
> Thanks for response.

duncan smith

unread,
Mar 29, 2011, 12:33:15 PM3/29/11
to wxpytho...@googlegroups.com
Hello,
Does anyone know if there is any way of controlling the ordering
of the buttons in a basic MessageDialog or MessageBox? e.g.

wx.MessageBox("Message", "Title", wx.YES_NO | wx.ICON_QUESTION)

produces a message box with the "No" button on the left, and to be
consistent with the rest of my application I would prefer it on the
right. I've tinkered with the style and tried wx.OK | wx.CANCEL hoping
it would give me the "OK" button on the left, but it doesn't.

Just checking if there's a simple solution without having to write my
own class. Cheers.

Duncan

Python 2.6, Ubuntu 9.10, wxPython 2.6.

Tim Roberts

unread,
Mar 29, 2011, 12:46:15 PM3/29/11
to wxpytho...@googlegroups.com

No, and in general you should not TRY to do so.

In Windows, wx.MessageBox calls the operating system's MessageBox API.
The MessageBox API displays those buttons in an order dictated by the
Microsoft Windows User Interface Guidelines, a book that reflects the
tens of hours of user interface research Microsoft has done over the
years, and which used to be required reading for application
developers. Users expect the buttons to be in the order you see.

Now that you've had the requisite scolding, it's pretty darned easy to
create your own MessageBox class. Or, check the wx.MessageDialog class.

Mike Driscoll

unread,
Mar 29, 2011, 2:56:36 PM3/29/11
to wxpytho...@googlegroups.com
Or just use the GenericMessageDialog: wx.lib.agw.genericmessagedialog. There's a little demo of it in this article:

http://www.blog.pythonlibrary.org/2010/07/10/the-dialogs-of-wxpython-part-2-of-2/

----------------
Mike Driscoll
http://www.blog.pythonlibrary.org

duncan smith

unread,
Mar 29, 2011, 10:14:10 PM3/29/11
to wxpytho...@googlegroups.com
Mike Driscoll wrote:
> Or just use the GenericMessageDialog: wx.lib.agw.genericmessagedialog.
> There's a little demo of it in this article:
>
> http://www.blog.pythonlibrary.org/2010/07/10/the-dialogs-of-wxpython-part-2-of-2/
>

Thanks, I wasn't aware of that. On Windows I get the button order I was
wanting, and that's the platform most of the users will be on. I've
changed the sizers on my other dialogs to wx.StdDialogButtonSizer
instances to get consistency within each platform, despite my personal
preference for 'Yes' to the left of 'No'. Only issue now is figuring
out why the buttons aren't centre-justified on XP. Cheers.

Duncan

Peter Damoc

unread,
Mar 30, 2011, 8:22:30 AM3/30/11
to wxpytho...@googlegroups.com
What problem are you trying to solve with this virtual keyboard?
Can you give use more details?

I might be wrong but I imagine that these details could change the
decisions you make dramatically.
Are you trying to recruit some of the available mobility in patients
to provide greater autonomy?
What kind of mobility? What kind of interface are you planning to use
with the patients?

As for being new to Python and wxPython, I envy you... :) you have so
many wonderful things to learn in front of you.
Peter

On Tue, Mar 29, 2011 at 6:26 AM, Ping <a222...@yahoo.com.tw> wrote:

> We are a bunch of college students trying to develop a cross-platform
> application which may assist the the ALS (Amyotrophic Lateral
> Sclerosis) patients in using computers. The application include the
> screen mouse and keyboard. As we are very new to Python and wxPython,
> lots of things are there for us to learn.


--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

duncan smith

unread,
Mar 31, 2011, 8:34:05 PM3/31/11
to wxpytho...@googlegroups.com
Hello,
I am trying to produce a ComboBox which allows the user to
distinguish between two different groups of items. My initial thought
was to use different coloured text, but I'm not sure that's possible.
Am I basically limited to a single font / colour / background colour for
every item in the list? The user should be able to select one, and only
one item, but be able to see which group the item belongs to. Much
googling has thrown up very little. Thanks.

Duncan

Mike Driscoll

unread,
Apr 1, 2011, 9:28:28 AM4/1/11
to wxpytho...@googlegroups.com
Hi,

Check out the ComboCtrl or the OwnerDrawnComboBox demos in the wxPython demo. I think one of those will do what you want.

- Mike

duncan smith

unread,
Apr 14, 2011, 11:27:15 AM4/14/11
to wxpytho...@googlegroups.com
Hello,
I have a grid where row labels will often be too long for the
label width, so I want the user to be able to see the full label by
moving the mouse over the labels. The following binding and handler
almost do the job. The only issue is that the tooltip stays in the same
position whilst I move the mouse over the row labels. If I move the
mouse out of the label window and then back the tooltip is positioned
(initially) where I would like it (at the mouse position). If I move
the mouse quickly over the labels the tooltip does occasionally appear
at a new location. It appears that what I could do is to post some
command event to force a repositioning when I move the mouse from one
label to another. I have tried a number of events, such as leaving the
row label window, but no luck so far. I don't want to accidentally hit
upon something that works for me (i.e. on Ubuntu), but not for the users
(almost certainly Windows). So, probably time to stop simply trying
things and ask for some advice. Any ideas? Thanks.

Duncan


self.GetGridRowLabelWindow().Bind(wx.EVT_MOTION,self.onMouseOverRowLabel)

def onMouseOverRowLabel(self, event):
x = event.GetX()
y = event.GetY()
row = self.YToRow(y)
win = self.GetGridRowLabelWindow()
win.SetToolTipString(self.GetRowLabelValue(row))
event.Skip()

Robin Dunn

unread,
Apr 14, 2011, 7:54:23 PM4/14/11
to wxpytho...@googlegroups.com
On 4/14/11 8:27 AM, duncan smith wrote:
> Hello,
> I have a grid where row labels will often be too long for the label
> width, so I want the user to be able to see the full label by moving the
> mouse over the labels. The following binding and handler almost do the
> job. The only issue is that the tooltip stays in the same position
> whilst I move the mouse over the row labels. If I move the mouse out of
> the label window and then back the tooltip is positioned (initially)
> where I would like it (at the mouse position). If I move the mouse
> quickly over the labels the tooltip does occasionally appear at a new
> location. It appears that what I could do is to post some command event
> to force a repositioning when I move the mouse from one label to
> another. I have tried a number of events, such as leaving the row label
> window, but no luck so far. I don't want to accidentally hit upon
> something that works for me (i.e. on Ubuntu), but not for the users
> (almost certainly Windows). So, probably time to stop simply trying
> things and ask for some advice. Any ideas? Thanks.

We don't have much (if any) control over how and where tooltips are
shown, but perhaps if you set the tooltip to "" first, and then reset it
to the new value (perhaps via CallAfter) then it will make a difference.
(BTW, you should probably be optimizing your code to only set the
tooltip value if the event is happening over a different row than where
it was shown the last time, otherwise you are setting the same value
many times as the mouse moves over the same cell.)

If that doesn't work then you could always use wx.TipWindow and show a
tooltip-like window yourself.


--
Robin Dunn
Software Craftsman
http://wxPython.org

Mike Driscoll

unread,
Apr 15, 2011, 11:27:57 AM4/15/11
to wxpytho...@googlegroups.com
You might also look at the supertooltip widget in the agw lib. It might be easier to configure.

- Mike

duncan smith

unread,
Apr 15, 2011, 12:39:53 PM4/15/11
to wxpytho...@googlegroups.com

Thanks. That might be my next port of call. Resetting the tooltip made
no difference and a wx.TipWindow was less responsive and caused my
application to crash in a fairly unpredictable way (and taking down IDLE
without providing a traceback). Possibly my fault, but I haven't really
got the time to investigate. Cheers.

Duncan

Vlastimil Brom

unread,
Apr 15, 2011, 4:41:03 PM4/15/11
to wxpytho...@googlegroups.com
2011/4/15 duncan smith <buz...@contactbox.co.uk>:

>>
>
> Thanks. That might be my next port of call. Resetting the tooltip made no
> difference and a wx.TipWindow was less responsive and caused my application
> to crash in a fairly unpredictable way (and taking down IDLE without
> providing a traceback).  Possibly my fault, but I haven't really got the
> time to investigate.  Cheers.
>
> Duncan
>

Hi,
if I understand correctly and you are running the code for a wx app
from within Idle, you are likely to get unpredictable errors anyway,
as wx and tkinter somehow interfere with each other imn such casees.

for emulating (simplyfied) toolstips, I was quite happy with using
wx.PopupWindow with a static text in it; you can check the rough
sample:

http://groups.google.com/group/wxpython-users/msg/39c3de5c8a8923e7

(The most complicated part deals with aui, the usage of PopupWindow
itself is rather straightforward.)

regards,
vbr

duncan smith

unread,
Apr 15, 2011, 8:11:18 PM4/15/11
to wxpytho...@googlegroups.com
Vlastimil Brom wrote:
> 2011/4/15 duncan smith <buz...@contactbox.co.uk>:
>> Thanks. That might be my next port of call. Resetting the tooltip made no
>> difference and a wx.TipWindow was less responsive and caused my application
>> to crash in a fairly unpredictable way (and taking down IDLE without
>> providing a traceback). Possibly my fault, but I haven't really got the
>> time to investigate. Cheers.
>>
>> Duncan
>>
>
> Hi,
> if I understand correctly and you are running the code for a wx app
> from within Idle, you are likely to get unpredictable errors anyway,
> as wx and tkinter somehow interfere with each other imn such casees.
>

That was just in an attempt to get a traceback. I'm actually using Boa
Constructor.

> for emulating (simplyfied) toolstips, I was quite happy with using
> wx.PopupWindow with a static text in it; you can check the rough
> sample:
>
> http://groups.google.com/group/wxpython-users/msg/39c3de5c8a8923e7
>

Thanks, I'll check it out.

Duncan

duncan smith

unread,
Jun 1, 2011, 1:31:48 PM6/1/11
to wxpytho...@googlegroups.com
Hello,
I have a simple problem (I think) for which I can't find a simple
solution. I have two types of item that I want the user to be able to
select from a combobox. (This is not the same issue I posted about a
couple of months ago.) I'd like to be able to add a separator as I would
for a menu. Just checking if there's a reasonably simple way of doing
that before I start looking at ComboCtrl and OwnerDrawnComboBox (or more
likely, just abandon the idea of separating the items). Cheers.

Duncan

Tim Roberts

unread,
Jun 1, 2011, 1:47:08 PM6/1/11
to wxpytho...@googlegroups.com

One workable but low-tech solution used by many web sites is to add the
string "----------------" where you want the separator.

Robin Dunn

unread,
Jun 1, 2011, 3:14:34 PM6/1/11
to wxpytho...@googlegroups.com

You could use a RadioBox to allow the user to select which type of item
to select, and then refill the combo with the items of that type.

duncan smith

unread,
May 4, 2012, 8:37:44 PM5/4/12
to wxpytho...@googlegroups.com
Hello,
I am having a few issues with the Treebook control on Windows XP.
I have written an application (Python 2.6, wxPython 2.8) that works fine
on Ubuntu 10.04, but I cannot get the same functionality on Windows.

I cannot edit the treebook labels by double clicking, even if I set the
wx.TR_EDIT_LABELS style on the underlying TreeCtrl instance (which seems
not to be needed on Linux).

I cannot delete a treebook page by hitting the delete key.

The treebook is not resized when pages are deleted (or on manual
resizing of the window). (In fact I've also managed to break this on
Ubuntu when stripping down the code to a reasonable length, although the
resize event generated in the OnEndEdit method does have the desired
effect. I'll investigate this. Nevertheless, my code that does work as
expected on Ubuntu does not on XP, so any hints would be appreciated.)
Code below. Thanks.

Duncan



############################code#########################

import wx

class Frame(wx.Frame):
def __init__(self, prnt):
wx.Frame.__init__(self, id=wx.ID_ANY, name='Frame', parent=prnt,
pos=wx.Point(280, 247), size=wx.Size(1288, 767),
style=wx.DEFAULT_FRAME_STYLE, title='Example')
self.SetClientSize(wx.Size(1280, 733))
self.SetMinSize(wx.Size(600, 400))
self.Bind(wx.EVT_KEY_UP, self.OnFrameKeyUp)
self.treebook = wx.Treebook(self, wx.ID_ANY,
style=wx.BK_DEFAULT)
self.treebook.Bind(wx.EVT_TREEBOOK_NODE_EXPANDED,
self.OnTreebook)
self.treebook.Bind(wx.EVT_TREEBOOK_NODE_COLLAPSED,
self.OnTreebook)
tc = self.treebook.GetTreeCtrl()
tc.SetWindowStyle(tc.GetWindowStyle()|wx.TR_EDIT_LABELS)
tc.Bind(wx.EVT_LEFT_DCLICK, self.OnFrameLeftDclick)
self.Bind(wx.EVT_TREE_END_LABEL_EDIT, self.OnEndEdit)
# add some panels
panel1 = wx.Panel(self.treebook)
panel2 = wx.Panel(self.treebook)
panel3 = wx.Panel(self.treebook)
self.treebook.AddPage(panel1, 'First')
self.treebook.AddPage(panel2, 'Second')
self.treebook.AddSubPage(panel3, 'Subpanel')
self.SendSizeEvent()

def OnTreebook(self, event):
self.SendSizeEvent()
event.Skip()

def OnFrameKeyUp(self, event):
if event.GetKeyCode() == wx.WXK_DELETE:
self.treebook.DeletePage(self.treebook.GetSelection())
event.Skip()

def OnFrameLeftDclick(self, event):
tc = event.GetEventObject()
item, flags = tc.HitTest(event.GetPosition())
if item:
print item
tc.EditLabel(item)
event.Skip()

def OnEndEdit(self, event):
wx.CallAfter(self.SendSizeEvent)
event.Skip()


app = wx.PySimpleApp()
frame = Frame(None)
frame.Show()
app.MainLoop()

#################################end code###########################

duncan smith

unread,
May 5, 2012, 1:11:32 PM5/5/12
to wxpytho...@googlegroups.com
[snip]

It seems that label editing for wx.ListCtrl in the wxPython (2.8.10.1)
demo does not work under Windows XP. Can anyone confirm whether or not
label editing is possible on XP? If not I need to redesign my
application (even if I can sort out the other issues). Thanks.

Duncan

duncan smith

unread,
May 6, 2012, 11:19:58 AM5/6/12
to wxpytho...@googlegroups.com
That should have been wx.TreeCtrl, not wx.ListCtrl. And setting the
appropriate style works. Regarding the wx.Treebook; after hours of
searching and trying various things I found this.

http://old.nabble.com/--wxwindows-Patches-1886815---wxTreebook---Allowing-label-edition-td15291536.html

It seems that the wx.TR_EDIT_LABELS style must be set when the
underlying tree control is created. But the programmer has no control
over the styles that are set. I assume that on Linux the relevant style
is set, and on Windows it isn't. So it seems that label editing is
simply not available on Windows, unless someone knows of a workaround.
If so, I'd appreciate it. Thanks.

Duncan

Robin Dunn

unread,
May 8, 2012, 12:11:52 PM5/8/12
to wxpytho...@googlegroups.com
On 5/6/12 8:19 AM, duncan smith wrote:
> On 05/05/12 18:11, duncan smith wrote:
>> [snip]
>>
>> It seems that label editing for wx.ListCtrl in the wxPython (2.8.10.1)
>> demo does not work under Windows XP. Can anyone confirm whether or not
>> label editing is possible on XP? If not I need to redesign my
>> application (even if I can sort out the other issues). Thanks.
>>
>> Duncan
>>
>
> That should have been wx.TreeCtrl, not wx.ListCtrl. And setting the
> appropriate style works. Regarding the wx.Treebook; after hours of
> searching and trying various things I found this.
>
> http://old.nabble.com/--wxwindows-Patches-1886815---wxTreebook---Allowing-label-edition-td15291536.html
>
>
> It seems that the wx.TR_EDIT_LABELS style must be set when the
> underlying tree control is created. But the programmer has no control
> over the styles that are set. I assume that on Linux the relevant style
> is set, and on Windows it isn't.

They are different implementations so more likely the wx.TreeCtrl used
on linux doesn't require the flag to be set before creation, or
something like that.

> So it seems that label editing is
> simply not available on Windows, unless someone knows of a workaround.
> If so, I'd appreciate it. Thanks.

It is not hard to implement a book-like class yourself. You simply need
to have your tree and the panels you want to display, hide all but one
of the panels, and put them all in a sizer. When a selection in the
tree is made then you hide the one currently shown, show the new one,
and call Layout so the sizer will resize or reposition the newly shown
panel if needed. This will give you more control over how the tree is
created, how it is sized, etc.

BTW, a comment from your sample code: Key events are sent only to the
widget that has the focus, and since they are not command events they do
not propagate up the window hierarchy. Since you were binding your key
handler to the frame then this is why the delete key did not do anything
for you.

duncan smith

unread,
May 12, 2012, 4:59:05 PM5/12/12
to wxpytho...@googlegroups.com
Unfortunately I've lost Robin's reply to this, but it helped. What I had
already started to implement as a solution is pasted below. I tried to
implement label editing by subclassing wx.Treebook and using a
wx.TextCtrl. It seems to work fairly well apart from two issues.

1. When editing, if I left click on the text control to place the
cursor, then I lose the control. i.e. I think the tree control is
processing the event and the text control is losing focus, triggering
its UpdateParent method. I could be wrong. I've tried a number of
things, but haven't been able to fix this.

2. After renaming (which works fine as long as I don't left click) I'd
like to resize the tree control. Elsewhere I bind events such as
wx.EVT_TREEBOOK_NODE_EXPANDED to a handler that simply sends a resize
event to the treebook, which works. But calling self.SendSizeEvent() at
the end of the UpdateLabel method doesn't resize the tree control to
reflect the edited text.

Any pointers appreciated. Thanks.

Duncan


###############################code##########################

import wx

import sys

class Treebook(wx.Treebook):
def __init__(self, *args, **kwargs):
wx.Treebook.__init__(self, *args, **kwargs)
self.text = TextCtrl(self)
self.text.Show(False)
self.item = None

def EditLabel(self, item):
# allow label editing on Windows
if sys.platform.startswith('win'):
self._EditLabel(item)
else:
wx.Treebook.GetTreeControl().EditLabel(self, item)

def _EditLabel(self, item):
self.item = item
tc = self.GetTreeCtrl()
self.text.SetValue(tc.GetItemText(item))
self.text.SetInsertionPointEnd()
self.text.SetSelection(-1, -1)
self.text.SetRect(tc.GetBoundingRect(item, textOnly=True))
self.text.Show(True)
self.text.SetFocus()

def UpdateLabel(self, s):
tc = self.GetTreeCtrl()
if not tc.GetItemText(self.item) == s:
tc.SetItemText(self.item, s)


class TextCtrl(wx.TextCtrl):
def __init__(self, *args, **kwargs):
wx.TextCtrl.__init__(self, *args, **kwargs)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_KILL_FOCUS, self.UpdateParent)
self.Bind(wx.EVT_TEXT_ENTER, self.UpdateParent)

def OnKeyDown(self, event):
keycode = event.GetKeyCode()
if keycode == wx.WXK_ESCAPE:
tb = self.GetParent()
self.SetValue(tb.GetItemText(tb.item))
event.Skip()

def UpdateParent(self, event):
self.GetParent().UpdateLabel(self.GetValue())
self.HideAndMove()

def HideAndMove(self):
self.Show(False)
self.SetPosition((0,0))
self.SetSize((0,0))


duncan smith

unread,
May 14, 2012, 11:34:06 AM5/14/12
to wxpytho...@googlegroups.com
On 12/05/12 21:59, duncan smith wrote:
> Unfortunately I've lost Robin's reply to this, but it helped. What I had
> already started to implement as a solution is pasted below. I tried to
> implement label editing by subclassing wx.Treebook and using a
> wx.TextCtrl. It seems to work fairly well apart from two issues.
>
> 1. When editing, if I left click on the text control to place the
> cursor, then I lose the control. i.e. I think the tree control is
> processing the event and the text control is losing focus, triggering
> its UpdateParent method. I could be wrong. I've tried a number of
> things, but haven't been able to fix this.
>
> 2. After renaming (which works fine as long as I don't left click) I'd
> like to resize the tree control. Elsewhere I bind events such as
> wx.EVT_TREEBOOK_NODE_EXPANDED to a handler that simply sends a resize
> event to the treebook, which works. But calling self.SendSizeEvent() at
> the end of the UpdateLabel method doesn't resize the tree control to
> reflect the edited text.
>

[snip]

OK. Making the underlying TreeCtrl the parent of the TextCtrl solves 1.
Still struggling with 2. though. Cheers.

Duncan

duncan smith

unread,
Jun 8, 2012, 10:34:18 PM6/8/12
to wxpytho...@googlegroups.com
Hello,
I have two wx applications, and want to be able to launch one
from the other (in a separate process). For this I need to identify the
path to one file from within the other file. Essentially I have
something along the lines of the two files below.

################App1.py################

import wxversion

wxversion.ensureMinimal('2.8')

import wx

import App2 # solely to retrieve the file path

print App2.__file__

#################end###################

################App2.py################

import wxversion

wxversion.ensureMinimal('2.8')

import wx

#################end###################


This fails with a wxversion.AlreadyImportedError

I can obviously search for App2.py rather than importing it and using
App2.__file__ to retrieve the path. But I was just wondering if there is
a neater way of handling this. Replacing,


wxversion.ensureMinimal('2.8')


with


try:
wxversion.ensureMinimal('2.8')
except wxversion.AlreadyImportedError:
pass


in *both* files appears to do the trick. However, I am a bit confused as
to why the exception is being raised in App1.py, rather than App2.py (or
at all really). I'd appreciate any advice before I start using try ...
except to allow the use of __file__. I imagine there are probably cases
(different versions required) where it would be a bad idea. TIA.

Duncan

Robin Dunn

unread,
Jun 11, 2012, 3:12:14 PM6/11/12
to wxpytho...@googlegroups.com
On 6/8/12 7:34 PM, duncan smith wrote:
> Hello,
> I have two wx applications, and want to be able to launch one from the
> other (in a separate process). For this I need to identify the path to
> one file from within the other file. Essentially I have something along
> the lines of the two files below.
>
> ################App1.py################
>
> import wxversion
>
> wxversion.ensureMinimal('2.8')
>
> import wx
>
> import App2 # solely to retrieve the file path
>
> print App2.__file__
>
> #################end###################
>
> ################App2.py################
>
> import wxversion
>
> wxversion.ensureMinimal('2.8')
>
> import wx
>
> #################end###################
>
>
> This fails with a wxversion.AlreadyImportedError
>
> I can obviously search for App2.py rather than importing it and using
> App2.__file__ to retrieve the path.

That's what I would do. Importing means that you are going to execute
code, which means that the state of the importing application is going
to change, even if the imported module doesn't really do anything
significant. Personally I would rather keep them as separate as possible.

DevPlayer

unread,
Jun 18, 2012, 2:02:33 AM6/18/12
to wxpytho...@googlegroups.com
Not the way I'd do it but you could, in each app module, try:
 
    if __name__ == "__main__":
        import wxversion
        wxversion.ensureMinimal('2.8')
        import wx
or:
    if __name__ <> "__main__":
        import sys
        sys.exit(0)
 
or something like on windows:
find app2.py | python.exe app1.py
 
if you're doing:
 
    import app2
 
then very likely app2.py is in the PYTHONPATH environment variable;
and you might get away with doing something like:
 
    found = [x for x in os.environ['PYTHONPATH'].split(os.pathsep) if "app2.py" in os.listdir(x)]
    if not found:
        found = [x for x in os.environ['PATH'].split(os.pathsep) if "app2.py" in os.listdir(x)]
 

Gadget/Steve

unread,
Jun 18, 2012, 2:55:59 AM6/18/12
to wxpytho...@googlegroups.com
Assuming that both App1 and App2 are packages you could do something like having a file called AppLocation.py in each package that just contains something along the lines of:

from sys.path import split
""" Define a value AppPath that gives a path to this package """
(AppPath, _dummy) = split(__file__)

Then in each of your two apps you could do:

from AppX.AppLocataion import AppPath

and take it from there, you could even construct the full application path in there for neatness.

Gadget/Steve



Reply all
Reply to author
Forward
0 new messages