Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to learn PyQt with knowledge from Tkinter

Maybe I'm jumping into the deep end, but I'll give it a shot.

Here are some useful features of Tkinter:

  • The Tkinter Canvas widget is an object oriented drawing canvas. The elements of the drawing are essentially widgets themselves, as they can be moved, modified, and bound to events.

  • Tkinter uses bindings to trigger callbacks. The event is passed as a string. Custom events can be easily created with event_generate.

  • Tkinter has the after method, which waits for a specified amount of time without freezing the GUI.

  • Tkinter has predefined fonts like TkDefaultFont, and colors like systemButtonFace, which are dependant on the system.

My questions are:

What are the pyQt equivalents of these features (especially the bold ones)?

How can I "bind" elements of a widget (e.g. the label of a checkbutton only) to an event?

like image 756
D K Avatar asked Sep 05 '11 19:09

D K


People also ask

Should I learn Tkinter or PyQt first?

If your main goal is to solidify your python knowledge, I would recommend Tkinter. It's simpler and it's already installed with Python. If you want to build complex applications, I recommend PyQt, which is way more powerful.

Is PyQt easier than Tkinter?

It requires a lot of time for understanding all the details of PyQt. Tkinter is easy to understand and master due to a small library. 3. PyQt has a modern look and a good UI.

Can you use Tkinter and PyQt together?

In order to have both the Tkinter and Qt versions working simultaneously, I had to fork the process - and start each toolkit in a separate running instance; Your case is not identical, as the Qt GUI will be already running, but maybe having this to start up with you can come along with a work-around.

Is PyQt better than Tkinter?

Tkinter is good, and more than suitable for simple tasks, but PyQt just offers more overall in terms of widgets and looks.


1 Answers

In Qt and PyQt events are called signals and you bind to them using slots (docs here). Generally speaking what you do define a slot with an @ decorator.

class WindowImpl (QtGui.QMainWindow, Ui_TremorMain, Ui_Graphs):
  def __init__ (self, buffer, parent = None, configuration = None):
    # do some initialisation here (not GUI setup however)

  @QtCore.pyqtSlot(int, name="on_confSelectorCombo_currentIndexChanged")
  def confChanged (self, newConf):
    # do some stuff here to handle the event

The above would be triggered on the currentIndexChanged event of an object called confSelectorCombo. The setup of the confSelectorCombo is done in the GUI builder or Qt Creator as Nokia has decided to call it. This really is what you want to use to get started. There's tutorials here on using Qt Creator. Obviously you'll want to go through the docs and see what signals are emitted by which widgets.

As for the font stuff all I know is what it says on the docs:

If you have not set a font for your application then the default font on your
machine will be used, and the default font can be different on different
machines. On Windows the default Windows font is used, on X11 the one in qtrc
can be used. If a default font can’t be found, then a font specified by Qt
will be used.

The QStyleSheet and QStyle act as proxies for changing the appearance of widgets (QStylesheet,QStyle).

As for making the application wait I found this

QTime dieTime = QTime::currentTime().addSecs(2);
while( QTime::currentTime() < dieTime ):
  QCoreApplication::processEvents(QEventLoop::AllEvents, 100);

There is also QThread.sleep() (docs), depending on what kind of an effect you want. Probably also worth looking at the threading support over at Qt docs

Overall in finding information about how to do stuff in PyQt I have found it surprisingly useful to look at the Qt documentation and then just writing the stuff in Python. 9 times out of 10 this works. On another note, it's probably also worth looking into PySide which is another python Qt library. I've haven't used myself before as it has been in the works previously but I noticed that they had released a 1.0.6 version.

UPDATE Just to reiterate Luke Woodward below, you can use QGraphicsScene and QGraphicsView to render stuff in an object oriented way. The QGraphicsScene doesn't actually render anything it just a scene graph, the QGraphicsView is then used to render the contents of the scene graph. For low level drawing there´s also QPainter - there's a basic drawing tutorial here. It's also worth looking at QGraphicsItem which is the base for all graphics items and

includes defining the item's geometry, collision detection, its painting     
implementation and item interaction through its event handlers

docs here. The Context2D provides an HTML canvas (if I'm not mistaken through the use of WebKit). The canvas itself only has a changed slot, but any objects you place on the canvas will/can have more slots. There's a fairly complete looking tutorial on Context2D and Context2DCanvas here. For an explanation as to why so many different ways of rendering stuff, you'll have to ask someone else. My two cents is that is has something to do with the fact that Qt is supposed to work everywhere and Trolltech and later Nokia wanted to provide lots of choice. Luckily the docs are really good.

like image 84
Matti Lyra Avatar answered Oct 03 '22 15:10

Matti Lyra