Trying to learn PyQt with knowledge from Tkinter

I may be jumping to the deep end, but I will give him a chance.

Here are some useful Tkinter features:

  • The Tkinter Canvas widget is an object-oriented canvas. Drawing elements are essentially widgets themselves, since they can be moved, modified, and attached to events .

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

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

  • Tkinter has predefined fonts like TkDefaultFont and colors like systemButtonFace , which are system dependent.

My questions:

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

How can I bind widget elements (for example, the label of only the checkbox button) to the event?

+6
source share
1 answer

In Qt and PyQt events, signals are called, and you connect them using slots ( docs here ). Generally speaking, you define a slot with the @ 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 will be triggered in the currentIndexChanged event of currentIndexChanged object named confSelectorCombo . confSelectorCombo is configured in the GUI builder or Qt Creator because Nokia decided to call it. This is really what you want to use to get started. Here's the use of Qt Creator. Obviously, you will want to go through the documents and see what signals are issued using widgets.

As for the font, all I know is what it says in 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. 

QStyleSheet and QStyle act as proxies to change the appearance of widgets ( QStylesheet , QStyle ).

As for waiting for the application, 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 effect you want. It's probably worth taking a look at streaming support on Qt docs as well.

In general, when looking for information on how to do things in PyQt, I found it surprisingly useful to look at the Qt documentation and then just write the material in Python. 9 times out of 10 it works. In another note, it's probably worth taking a look at PySide, which is another Qt python library. I have not used myself before, as before, but I noticed that they released version 1.0.6.

UPDATE To repeat Luke Woodward below, you can use QGraphicsScene and QGraphicsView to render material in an object-oriented manner. QGraphicsScene does not actually display anything just the scene graph, then QGraphicsView used to render the contents of the scene graph. For low-level drawing, there is also a QPainter - there is a basic drawing tutorial here . Also worth a look is QGraphicsItem , which is the base for all graphic elements and

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

docs here . Context2D provides an HTML canvas (if I'm not mistaken when using WebKit). The canvas itself has only a modified slot, but any objects that you place on the canvas will have / can have more slots. Here's a pretty complete tutorial on Context2D and Context2DCanvas here . To explain why there are so many different ways to present material, you will have to ask someone else. My two cents is that Qt should work everywhere, and Trolltech, and then Nokia wants to provide a lot of choice. Fortunately, the docs are really good.

+1
source

Source: https://habr.com/ru/post/896661/


All Articles