I am just putting some stuff here right now, because you have such nice color coding in your wiki. Maybe I'll turn it into a tutorial if I figure out what I am doing.
I found this snippet here (french). I don't know if this is the right way to do it, but it seems to work.
from PyQt4 import QtGui, uic
app = QtGui.QApplication(sys.argv) widget = uic.loadUi("demo.ui") widget.show()
app.exec_() Of course you will need to make a ui file called demo.ui for this to work.
This code really works.
from PyQt4 import QtGui
from PyQt4 import uic
from PyQt4 import QtCore
def __init__(self, *args): QtGui.QMainWindow.__init__(self, *args)
# just pass self uic.loadUi("mainWindow.ui", self)
#this line makes sure that we are only looking for a string #if we want a bool as well we can add @QtCore.pyqtSignature("bool") @QtCore.pyqtSignature("") def on_button1_clicked(self): print "You Clicked the button!" @QtCore.pyqtSignature("") def on_button2_clicked(self): print "You Clicked the button2!"
app = QtGui.QApplication(sys.argv) widget = DemoImpl() widget.show() app.exec_()
You will need this file too.
Once you have it in a variable, you can throw the whole power of python at it. I have tried to show how to do this in the simplest way possible. Note: This code shamelessly stolen from xpathevaluator. Lots of good pyqt4 sample code in there. Also might be helpful if you want to evaluate an xpath. But I'm not sure why you would want to do that.
#Load the widget
from PyQt4.QtGui import QWidget #This is a widget, not a window, and that makes a difference from PyQt4.uic import loadUiType #We need this to dynamically load the .ui file we created in qt designer from PyQt4.QtCore import pyqtSignature, Qt #Not sure what this signature thing is, but apparently it is great for buttons (signals/slots?) import sys
(form, formbase) = loadUiType('readinput.ui') #this is the .ui file we want to show
class readInputWidget(QWidget, form): #in this class, we load the window, and assign actions to the buttons
def __init__(self, parent=None): QWidget.__init__(self, parent) self.setAttribute(Qt.WA_DeleteOnClose) #This doesn't seem to change anything if you delete it, but may be usefull for housekeeping self.setupUi(self) @pyqtSignature("on_exitButton_clicked()") def on_exitButton_clicked(self): self.close() @pyqtSignature("on_evalButton_clicked()") def on_evalButton_clicked(self): print 'This should echo our text:' userinput = self.inputBox1.text() #This is the fantastic line of code we were waiting for! Make sure you have an input box by that name, and specify that it is text print userinput
from PyQt4.QtGui import QApplication from PyQt4.QtCore import Qt app = QApplication(sys.argv) widget = readInputWidget() widget.show() return app.exec_()
if __name__ == "__main__":
You will also need the ui file that goes along with that.