Handling delete event in pygtk/glade - python

I have a GUI designed in glade, using python/gtk in the background.I want to handle the delete event and display a "Are you sure?"-message dialog.I have been trying to handle the delete and destroy events, but failing to do so.any light?
#!/usr/bin/python
import .... stuff
class App:
def __init__(self):
self.gladefile = 'test.glade'
windowname = 'window'# This must match the window name in glade
self.wTree = gtk.glade.XML(self.gladefile, windowname)# object for acessing widgets
dic={
# Also need to set project2's signal tab
'on_window_delete_event':self.on_erro,
'on_window_destroy_event':self.on_erro,
}
self.wTree.signal_autoconnect (dic)
self.op=self.wTree.get_widget('window')
self.op.show()
def on_erro(self,widget,*args):
print 'hello'
app = App()
gtk.main()
This code opens a simple window .On clicking on close button, it prints hello and exits.(I want the window to remain open)

You have to return True in order to stop propagation of the delete event in the callback on_erro as mentioned in the documentation for "delete-event". In your current code, the callback is not returning any boolean value as required by the function, which I am guessing is returning False (Please check the signature for on_window_delete_event callback functions, the return type is boolean)
Hope this helps!

Related

How to record multiple checkboxes in wxPython?

I am trying to get my program to record if the user clicked on checkbox(es) so I know what things to delete. The problem is that I don't have all the variables names for all the checkboxes so I can't just do GetValue(). In the code below, I have a set number of things but in the actual program, it pulls user info from file.
import wx
class supper(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Delete Stocks',size=(300,300))
nexus=wx.Panel(self)
again=30
newfp='GOOG AAPL'.split()
#Need to see if user checked box and which one
for i in newfp:
self.checkbox=(wx.CheckBox(nexus,-1,i,(30,again),(160,-1)))
self.Bind(wx.EVT_CHECKBOX, self.lol,self.checkbox)
again+=30
anothercancel=wx.Button(nexus,label='Cancel',pos=(50,250),size=(60,40))
self.Bind(wx.EVT_BUTTON,self.acancel,anothercancel)
anotherok=wx.Button(nexus,label='OK',pos=(200,250),size=(60,40))
self.Bind(wx.EVT_BUTTON,self.okalready,anotherok)
def acancel(self,event):
self.Destroy()
#Couldn't figure out how to see what check was clicked
def okalready(self,event):
for i in newfp:
valuechecker=self.checkbox.GetValue()
if self.checkbox.Get()==True:
print 'You clicked this %s one'%i
self.Destroy()
def lol(self,event):
pass
if __name__=='__main__':
ok=wx.PySimpleApp()
people=supper(parent=None,id=-1)
people.Show()
ok.MainLoop()
This isn't the whole thing so there might be a variable that is not defined here. Thanks in advance! Look forward to the answers!
just keep them in a list ...
self.checkboxes = []
for i in newfp:
self.checkboxes.append(wx.CheckBox(nexus,-1,i,(30,again),(160,-1)))
self.checkboxes[-1].Bind(wx.EVT_CHECKBOX, self.lol)
again+=30
then to check which boxes are checked when you click ok you can use this
def okalready(self,event):
for i,cb in enumerate(self.checkboxes):
print "CB:%d = %s"%(i,cb.GetValue())
print "Checked:",[cb.GetLabel() for cb in self.checkboxes if cb.GetValue()]
self.Destroy()

How to wait for execution in other window in python

I have a program which will check for several conditions and check whether data is available or not. If there is any missing data then i will popup another window which will collect data in that window. It will have two buttons (Apply and Close). I want to return a value after button is triggered.
Both program.py and dataEntry.py have there own UIs designed in PyQt Disigner.
I want my program to wait for return value from other window. and depending on the input i will continue my other process.
Lets say program file is program.py and another window is dataEntry.py is imported in program.py
My dataEntry.py looks like
#imports necessary modules
class dataEntry(QtGui.QMainWindow,Ui_DataEntry):
def __init__(self):
super(dataEntry,self).__init__()
self.setupUi(self)
self.Btn_Apply.clicked.connect(self.ApplyChanges)
self.Btn_Close.clicked.connect(self.CancelChanges)
def ApplyChanges(self):
#This will trigger when ApplyButonn is clicked
#I want to return True value from here
return True
def CancelChanges(self):
#This will trigger when CancelButonn is clicked
#I want to return False value from here
return False
My program.py looks like
from dataEntry import dataEntry
class MainApp(QtGui.QMainWindow,Ui_MainApp):
def __init__(self):
super(MainApp,self).__init__()
self.setupUi(self)
self.CheckDetails()
def CheckDetails(self):
#Here i will check if required data is present else i will take data from dataEntry class
if checkFails:
input = dataEntry()
input.show()
#Here i want this class to wait until i get the result from dataEntry class
EntryResult = return value from dataEntry
if EntryResult:
#Do some thing when its True
else:
#Do some thing when its False
first of all import dataentry.py in program.py,
after this in your program.py
EntryResult = dataentry.py.Ui_DataEntry() #considering that the UI class in dataentry.py is Ui_DataEntry
This will run the dataentry.py and make your dataentry.py return the required value. This way, the program.py will wait till it gets a value from dataentry.py

PyQt: QtGui.QFileDialog.getSaveFileName won't close after selection

In my PyQt4 application, there is a functionality that allows users to save a avi file.
To this aim, a saveMovie method has been implemented in the main window:
def saveMovie(self):
""" Let the user make a movie out of the current experiment. """
filename = QtGui.QFileDialog.getSaveFileName(self, "Export Movie", "",
'AVI Movie File (*.avi)')
if filename != "":
dialog = QtGui.QProgressDialog('',
QtCore.QString(),
0, 100,
self,
QtCore.Qt.Dialog |
QtCore.Qt.WindowTitleHint)
dialog.setWindowModality(QtCore.Qt.WindowModal)
dialog.setWindowTitle('Exporting Movie')
dialog.setLabelText('Resampling...')
dialog.show()
make_movie(self.appStatus, filename, dialog)
dialog.close()
My idea is to use a QProgressDialog to show how the video encoding work is proceeding.
Nevertheless, after the selection of the filename, the QFileDialog won't disappear and the entire application stays unresponsive until the make_movie function has completed.
What should I do to avoid this?
Lesson learned: if you have some long-running operations to do -- for example, reading or writing a big file, move them to another thread or they will freeze the UI.
Therefore, I created a subclass of QThread, MovieMaker, whose run method encapsulates the functionality previosly implemented by make_movie:
class MovieMaker(QThread):
def __init__(self, uAppStatus, uFilename):
QtCore.QThread.__init__(self, parent=None)
self.appStatus = uAppStatus
self.filename = uFilename
def run(self):
## make the movie and save it on file
Let's move back to the saveMovie method. Here, I replaced the original call to make_movie with the following code:
self.mm = MovieMaker(self.appStatus,
filename)
self.connect(self.mm, QtCore.SIGNAL("Progress(int)"),
self.updateProgressDialog)
self.mm.start()
Note how I defined a new signal, Progress(int).
Such a signal is emitted by the MovieMaker thread to update the QProgressDialog used to show the user how the movie encoding work is progressing.

Extending QGIS Plugin builder result with a Combobox fails in referencing the new object

I have extended the UI file resulting from the Plugin builder with Qt Creator.
Just added some checkboxes and a combobox, named layercombo to the form.
The application is named jacktest.py. It uses an intermediate file jackdialog.py (generated from the plugin builder, left unchanged).
Compiled the UI file and the resource file. Then added some code to the plugin and tested this. It's no problem to get the available layer names in a QMessagebox. But how to add these to the combobox ?
Should be simple, but no option succeeds in referencing the combobox.
Error message: AttributeError: jacktest instance has no attribute 'layercombo'.
Result from my latest try:
# run method that performs all the real work
def run(self):
# create and show the dialog
dlg = jacktestDialog()
# show the dialog
dlg.show()
result = dlg.exec_()
for layer in self.iface.legendInterface().layers():
if layer.type() == QgsMapLayer.VectorLayer:
QMessageBox.information( self.iface.mainWindow(), "Info", layer.name())
self.layercombo.Items.Insert(0, layer.name())
# See if OK was pressed
if result == 1:
# do something useful (delete the line containing pass and
# substitute with your code
pass
You are trying to reference the current class (which is not your dialog) when you are setting the layercombo items
Replace:
self.layercombo.Items.Insert(0, layer.name())
with
dlg.ui.layercombo.Items.Insert(0, layer.name())
but you code still won't work correctly as exec_() is blocking and waits until it returns so you are adding items to an invisible dialog.
Try this instead:
# create and show the dialog
dlg = jacktestDialog()
# show the dialog
for layer in self.iface.legendInterface().layers():
if layer.type() == QgsMapLayer.VectorLayer:
QMessageBox.information( self.iface.mainWindow(), "Info", layer.name())
dlg.ui.layercombo.Items.Insert(0, layer.name())
result = dlg.exec_()
Went on in developing a Signal within the run module (code: def run (self):)
QObject.connect(dlg.ui.layercombo,SIGNAL('currentIndexChanged (int)'),self.select_one)
and the code of select_one is:
def select_one(self):
comboindex = dlg.ui.layercombo.currentIndex()
QMessageBox.information(self.iface.mainWindow(), "Info", comboindex)
Error message:
comboindex = dlg.ui.layercombo.currentIndex()
NameError: global name 'dlg' is not defined
Suppose I have to reference dlg as a parameter in the function call, but this is not working until now.

Simple, versatile and re-usable entry dialog (sometimes referred to as input dialog) in PyGTK

I am searching for a simple dialog with a text entry widget asking the user for some input. The dialog should be easy to run (like the gtk.MessageDialog variants) and as flexible.
There are of course some examples but they are either not flexible enough or too complicated to construct for my taste.
I hate re-inventing the wheel... or a dialog.
Based on an example I found (thanks Ardoris!), I came up with a dialog subclass... hope it helps someone out there!
#!/usr/bin/env python
import gtk
class EntryDialog(gtk.MessageDialog):
def __init__(self, *args, **kwargs):
'''
Creates a new EntryDialog. Takes all the arguments of the usual
MessageDialog constructor plus one optional named argument
"default_value" to specify the initial contents of the entry.
'''
if 'default_value' in kwargs:
default_value = kwargs['default_value']
del kwargs['default_value']
else:
default_value = ''
super(EntryDialog, self).__init__(*args, **kwargs)
entry = gtk.Entry()
entry.set_text(str(default_value))
entry.connect("activate",
lambda ent, dlg, resp: dlg.response(resp),
self, gtk.RESPONSE_OK)
self.vbox.pack_end(entry, True, True, 0)
self.vbox.show_all()
self.entry = entry
def set_value(self, text):
self.entry.set_text(text)
def run(self):
result = super(EntryDialog, self).run()
if result == gtk.RESPONSE_OK:
text = self.entry.get_text()
else:
text = None
return text
The run() method returns either the text entered in the entry box if the user presses <Enter> or clicks Ok. If Cancel is clicked or <Esc> pressed, the run() method returns None.
Except for that, the dialog should behave as any other gtk.MessageDialog instance.
Maybe that is not very general as it assumes you will always have Ok as an option, but that is what I need in 99% of my use cases anyway.
There isn't one available in GTK+. You've got two options:
Create a dialog, pack the Entry and any other content you need (probably the best way in my opinion)
Retrieve the content_area of the MessageDialog and append an Entry to it.
Something along the lines of:
#!/usr/bin/env python
import gtk
messagedialog = gtk.MessageDialog(parent=None, flags=0, type=gtk.MESSAGE_QUESTION, buttons=gtk.BUTTONS_OK, message_format="Hello")
action_area = messagedialog.get_content_area()
entry = gtk.Entry()
action_area.pack_start(entry)
messagedialog.show_all()
messagedialog.run()
messagedialog.destroy()
Though it does probably need more refinement to get the Entry to display nicely.
Here's the function I wrote, based on the previous answers here. It's a function instead of a class, which means you can use it in one line.
def get_text(parent, message, default=''):
"""
Display a dialog with a text entry.
Returns the text, or None if canceled.
"""
d = gtk.MessageDialog(parent,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
gtk.MESSAGE_QUESTION,
gtk.BUTTONS_OK_CANCEL,
message)
entry = gtk.Entry()
entry.set_text(default)
entry.show()
d.vbox.pack_end(entry)
entry.connect('activate', lambda _: d.response(gtk.RESPONSE_OK))
d.set_default_response(gtk.RESPONSE_OK)
r = d.run()
text = entry.get_text().decode('utf8')
d.destroy()
if r == gtk.RESPONSE_OK:
return text
else:
return None

Categories