wxPython with ScriptingBridge causes Seg Fault on exit - python

This is a very odd situation that I can not seem to figure out. I'm writing an application with wxPython on OSX Lion (wxpython version 2,9,3,1).
Whenever I import from ScriptingBridge and do something as simple as create a frame and then close it, it does not close cleanly and seg faults upon closing. Give this code a try:
from ScriptingBridge import *
import wx
class Test(wx.Frame):
def __init__(self, parent, title):
super(Test, self).__init__(parent, title=title, size=(300, 200))
self.Show()
app = wx.App()
Test(None, 'Hello')
app.MainLoop()
After running this code, just simply close it. It should seg fault. Now, simply comment out the ScriptingBridge import and run it again and close it. No Seg Fault.
What makes this even stranger is that if you just create a script that imports ScriptingBridge and yet does not import wxPython, once the script exits, it will not seg fault... It appears it's a combination between the two.
Has anyone seen this and have they found a work around for it? I'd like my app to exit cleanly if possible.

Related

Pyside UI pops up and then disappears when run from cmd before it can be used?

I am fairly new to python and have made a simple UI using pyside. When run from inside the anaconda IDE the UI works fine, but when I run it using anaconda from the command line \python.exe 'runquacker.py' the UI flashes up and disappears immediately.
The initial script is:
from PySide.QtCore import *
from PySide.QtGui import *
import sys
import quacker
class MainDialog(QDialog, quacker.Ui_Dialog):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
app = QApplication(sys.argv)
form = MainDialog()
form.show()
The rest of the UI is in quacker.py which collects a bunch of variables from the user, before executing a further analysis.py program using a subprocesscall. The variables are all passed in this way because thats the only way I could get pyside to work with my script!
e.g. For two variables 'plots' and 'block':
subprocess.call([sys.executable, 'd:\\py\\anaconda\\analysis.py', str(plots), str(block)], shell=True)
Ive tried putting a raw_input('Blah..') in a few places but it just causes the program to hang or nothing at all.
Using \python.exe -i runquacker.py also causes the program to hang.
Thanks
You need to add this line at the end of your script: app.exec_()
That's because you need to actually execute your Qt application if you whant to see something.
I'm not pretty sure why it works in Anaconda but if you are using some IDE like Spyder I think it works because Spyder is already running in Qt (so it called QApplication.exec_ before).

Can I access attributes of my wxpython frame from the IDLE Console?

Let's say I have a frame
class Frame(wx.Frame):
def __init__(self, *args, **kwargs):
super(Frame, self).__init__(*args, **kwargs)
self.InitUI()
self.SetSize((380,340))
self.Show()
self.something = 0
Which I start like so:
if __name__ == '__main__':
app = wx.App()
frame = Frame(None)
app.MainLoop()
And during debugging I find a bug I think is related to self.something. Can I view the contents self.something through IDLE's Console?
For you would find it worth looking for an IDE with built in debugger, there are several good free ones.
Alternatively you could use winpdb this would give you a full debugger as a standalone and it works fine with wxPython.
It is also worth looking at the python documents on debugging as you can open a debug console from within your code.
There is also the wx inspection tool, in your code try:
import wx.lib.inspection
wx.lib.inspection.InspectionTool().Show()
I doubt it. You need some kind of debugger that can pause the main process so you can take a look under the hood. I've heard that PyDev/Eclipse works and I know WingWare's IDE has a debugger that works with wx as I use it all the time. I haven't found any hard data about whether or not Python debugger (pdb) can attach itself to wx or not.
You might find the following threads useful though:
http://wxpython-users.1045709.n5.nabble.com/Simple-pdb-debugging-from-shell-td2305039.html
https://groups.google.com/forum/#!topic/wxpython-users/jO7YzV-wVkI
https://groups.google.com/forum/#!msg/wxpython-users/0T6hmqSkfW0/WIPqD3PkcdcJ

Python, wxPython error

Can anybody help me figure out what I'm doing wrong I've very little experience with GUIs.
code:
import wx
class bucky(wx.Frame):
#constructor
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Frame aka window',size=(300,200))
panel=wx.Panel(self)
button=wx.Button(panel,label="exit",pos=(130,10),size=(60,60))
self.Bind(wx.EVT_BUTTON, self.closebutton,button)
self.Bind(wx.EVT_CLOSE, self.closewindow)
def closebutton(self,event):
self.close(True)
def closewindow(self,event):
self.Destroy()
if __name__=='__main__':
app=wx.PySimpleApp()
frame=bucky(parent=None,id=-1)
frame.Show()
app.MainLoop()
Error:
PyNoAppError: The wx.App object must be created first!
win32ui.error: Error in Command Message handler for command ID 36864, Code 0
This code runs for me on Windows 7 with wxPython 2.8.12.1 and Python 2.6.6. What OS and Python versions are you using? I've seen this error message from time to time when I've run my code in IDLE. If you're doing that, then don't. Tkinter's mainloop (which is what IDLE is made with) will interfere with other GUI toolkit's main loops.
There is an issue in the closebutton method in that it call a "close" method which does not exist.

Different behaviour between python console and python script

I am experiencing different behaviour on the same code using the python console and a python script.
The code is as follows:
import gtk
import webkit
win = gtk.Window()
win.show()
web = webkit.WebView()
win.add(web)
web.show()
web.open("http://www.google.com")
When running the code in the python console, the output is a new frame that contains the google main page.
When running the code as a script, the result is a void frame. It closes very fast but even if I use a delay function, the webkit is not added to the frame.
How is it possible?
Furthermore, using PyDev IDE it flags: "unresolved import: gtk",
but if i run the project, the program starts without problem of compilation. is it normal?
Add
gtk.main()
to the end of your script. This starts the gtk event loop.
import gtk
import webkit
class App(object):
def __init__(self):
win = gtk.Window()
win.connect("destroy", self.destroy)
web = webkit.WebView()
web.open("http://www.google.com")
win.add(web)
web.show()
win.show()
def destroy(self, widget, data = None):
gtk.main_quit()
app = App()
gtk.main()
My guess is that the console keeps the python session open, while at the end of the script the program closes. When the script closes, it takes everything it created with it.
Something to test this theory: if you type "exit" in the console do you see the interface shut down in the same manner? If so, think of some code (e.g. a pause like a raw_input) that will allow the script to stay open.
Good luck!

Interacting with a gtk.container while gtk.main() is executing?

Experimenting with a battery monitor icon at the moment in Python using pygtk and egg.trayicon to create an icon to display a battery icon/tooltip.
I seem to be able to add the icon and the tooltip text, but when it then reaches the gtk.main() stage I need a way to modify these so it can then show the updated values.
I've tried gobject.idle_add() and gobject.timeout_add() without much luck, not sure where to go from this.
Anyone got any ideas?
EDIT: Perhaps not the clearest of questions.
I need to loop, fetching information from acpi while running and apply it to widgets inside the gtk container.
EDIT 2: Ok, it's properly down now. The issue was that I wasn't returning anything inside my callback. I just gave it "return 123" and now it's happily chugging away in my system tray, notifying me of my battery percentage :)
This example works for me:
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
import gobject
import gtk
from egg import trayicon
label = gtk.Label("Over here")
def callback(widget, ev):
label.set_text("You found me")
def timeout():
label.set_text("What are you waiting for?")
tray = trayicon.TrayIcon("TrayIcon")
box = gtk.EventBox()
box.add(label)
tray.add(box)
tray.show_all()
box.connect("button-press-event", callback)
gobject.timeout_add(3000L, timeout)
gtk.main()
Without seeing your code, it's hard to tell what doesn't work.

Categories