尝试用 Tkinter 的知识来学习 PyQt

发布于 2024-12-02 19:29:25 字数 568 浏览 1 评论 0 原文

也许我正在跳入深渊,但我会尝试一下。

以下是 Tkinter 的一些有用功能:

  • Tkinter Canvas 小部件是面向对象的绘图画布。绘图的元素本质上是小部件本身,因为它们可以移动、修改和绑定到事件

  • Tkinter 使用绑定来触发回调。事件作为字符串传递。可以使用 event_generate 轻松创建自定义事件。

  • Tkinter 有 after 方法,该方法等待指定的时间而不冻结 GUI。

  • Tkinter 具有预定义的字体(例如 TkDefaultFont)和颜色(例如 systemButtonFace),它们取决于系统

我的问题是:

这些功能(尤其是粗体功能)的 pyQt 等价物是什么?

如何将小部件的元素(例如仅复选按钮的标签)“绑定”到事件?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的軌跡 2024-12-09 19:29:25

在 Qt 和 PyQt 中,事件被称为信号,您可以使用插槽绑定到它们(此处的文档)。一般来说,您所做的事情是使用 @ 装饰器定义插槽。

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

上述内容将在名为 confSelectorCombo 的对象的 currentIndexChanged 事件上触发。 confSelectorCombo 的设置是在 GUI 构建器或诺基亚决定称之为的 Qt Creator 中完成的。这确实是您想要用来开始的。 此处提供了有关使用 Qt Creator 的教程。显然,您需要浏览文档并查看哪些小部件发出哪些信号。

上的内容:

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.

至于字体,我只知道文档 code>QStyleSheet 和 QStyle 充当更改小部件外观的代理(QStylesheet,QStyle)。

至于让应用程序等待我发现这个

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

还有QThread.sleep() (docs),具体取决于您想要什么样的效果。可能还值得查看 Qt docs 上的线程支持

总体查找信息关于如何在 PyQt 中做事,我发现查看 Qt 文档然后用 Python 编写这些东西非常有用。十有八九这个方法有效。另一方面,PySide 可能也值得研究,它是另一个 python Qt 库。我以前没有使用过自己,因为之前已经在工作中,但我注意到他们已经发布了 1.0.6 版本。

更新
下面重申一下 Luke Woodward,您可以使用 QGraphicsScene 和 QGraphicsView 以面向对象的方式渲染内容。 QGraphicsScene 实际上并不渲染任何东西,它只是一个场景图,然后使用 QGraphicsView 来渲染场景图的内容。对于低级绘图,还有 QPainter - 有一个基本绘图教程 此处。还值得一看 QGraphicsItem 它是所有图形项目的基础和

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

这里。要解释为什么有这么多不同的渲染方式,您必须询问其他人。我的两点意见是,这与 Qt 应该在任何地方都能工作这一事实有关,而 Trolltech 和后来的诺基亚希望提供很多选择。幸运的是,文档非常好。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文