Qt Jambi on Jython
In my previous post, Hello World! Qt Jambi, I showed you my brief experience with GUI toolkits for Python, and my idea to integrate Qt Jambi with Jython. Thus, we can take advantage of both Java and Python to use Qt library in Python. This is possible because Jython is an implementation of Python written in Java.
I’ve heard about Jython about over one year ago, but at that time, Jython only supported Python 2.2 features while Python 2.4 was already released for a long time, and because there is a big difference between Python 2.2 and Python 2.4, I never thought Jython is my option. But something is different, the Jython development team has released Jython 2.5 about two months ago and it seems that Jython is extremely useful for me now, to integrate with Qt Jambi.
To illustrate my idea of using Qt Jambi library in Jython, I firstly wrote the Jython equivalent code to the “Hello World” example written in Java in my previous post. Here is the original Java source code:
import com.trolltech.qt.gui.*;
public class HelloWorld
{
public static void main(String[] args) {
QApplication.initialize(args);
QPushButton hello = new QPushButton("Hello World!");
hello.resize(120, 40);
hello.setWindowTitle("Hello World");
hello.show();
QApplication.exec();
}
}
And here is the Jython equivalent to Java:
import sys
from com.trolltech.qt.gui import *
def main():
QApplication.initialize(sys.argv)
hello = QPushButton("Hello World!")
hello.resize(120, 40)
hello.setWindowTitle("Hello World")
hello.show()
QApplication.exec()
if __name__ == '__main__':
main()
To execute this script, I just simply run the following instruction, and the result should be nothing different.
jython -J-d32 -J-XstartOnFirstThread helloworld.py
To further demonstrate if signals are working correctly in Jython, I borrowed the source code from tutorial 2 in Qt Jambi official documentation. Here’s the original Java source code:
import com.trolltech.qt.gui.*;
public class Quit
{
public static void main(String args[])
{
QApplication.initialize(args);
QPushButton quit = new QPushButton("Quit");
quit.resize(80, 40);
quit.setFont(new QFont("Times", 18, QFont.Weight.Bold.value()));
quit.clicked.connect(QApplication.instance(), "quit()");
quit.setWindowTitle("Calling It Quits");
quit.show();
QApplication.exec();
}
}
And here’s the Jython equivalent I rewrote:
import sys
from com.trolltech.qt.gui import *
def main():
QApplication.initialize(sys.argv)
quit = QPushButton("Quit")
quit.resize(80, 40)
quit.setFont(QFont("Times", 18, QFont.Weight.Bold.value()))
quit.clicked.connect(QApplication.instance(), "quit()")
quit.setWindowTitle("Calling It Quits")
quit.show()
QApplication.exec()
if __name__ == '__main__':
main()
As usual, I executed the script by running instruction like this:
jython -J-d32 -J-XstartOnFirstThread quit.py

As you can see the result above, everything is just working fine. As long as I click the Quit button in the window, it quits normally, nothing wrong happened. Perfect!
Note that I add `-J-XstartOnFirstThread` parameter because I use Mac OS X operating system and I add `-J-d32` parameter because I use “Snow Leopard”. You don’t need these parameters if you are not a Mac user.
Filed under: Java, Jython, Mac, Programming, Qt | 5 Comments
Tags: Qt, Qt Jambi, Programming, Python, tutorial, GUI, toolkit, Jython




Hi,
I tried to play with QtJambi 4.5.2 and Jython 2.5.1 to see if I can get a simple demo up and running. unfortunately I couldn’t get a QPushButton to connect to a method. I’ve tried to add jython extension to QtJambi but still no way to get the connection.
This is what I’ve done:
from com.trolltech.qt.gui import *
from com.trolltech.extensions.jython import *
class MyFirstJythonJambiApp(QWidget):
def __init__(self):
hbox = QHBoxLayout()
hi = QPushButton(“hi!”, self)
quit = QPushButton(‘Quit’, self)
hbox.addWidget(hi)
hbox.addWidget(quit)
# I can’t get this oneto connect
hi.clicked.connect(self,”self.inform_user()”)
quit.clicked.connect(self, “close()”)
self.setWindowTitle(“Jython 2.5.1 and QtJambi 4.5.2 Demo”)
self.setLayout(hbox)
self.resize(340, 30)
def inform_user(self):
”)
QMessageBox.warning(self, “Hi!”, “This is a Jython/QtJambi Demo
QApplication([])
form = MyFirstJythonJambiApp()
form.show()
QApplication.exec()
Any idea how to get this working? I’ll be very grateful
Signals and slots are a bit tricky when using Qt Jambi with Jython, Qt has released a project called Qt Jambi Jython Bindings to relax this situation. However, the project is dead and the latest release doesn’t work well. I tried to fix the project and it works then. But due to its GPL license, I finally decide to give up this project.
I also tried to write pure Java with Qt Jambi, but I’m still not satisfied with it. My final choice is the native C++ Qt library. It beats any other programming language binding in my opinion.
However, if you still need my fixed version of the Qt Jambi Jython Bindings. Please give me your email address by sending an email to me.
Thanks
I didn’t spot your email address, so here is mine: stuntgp2000 [a t] gmail [d o t] com
Thank you very much!
oops, the indentation was removed by wordpress :S