也许我正在跳入深渊,但我会尝试一下。
以下是 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?
发布评论
评论(1)
在 Qt 和 PyQt 中,事件被称为信号,您可以使用插槽绑定到它们(此处的文档)。一般来说,您所做的事情是使用
@
装饰器定义插槽。上述内容将在名为
confSelectorCombo
的对象的currentIndexChanged
事件上触发。confSelectorCombo
的设置是在 GUI 构建器或诺基亚决定称之为的Qt Creator
中完成的。这确实是您想要用来开始的。 此处提供了有关使用 Qt Creator 的教程。显然,您需要浏览文档并查看哪些小部件发出哪些信号。上的内容:
至于字体,我只知道文档 code>QStyleSheet 和
QStyle
充当更改小部件外观的代理(QStylesheet,QStyle)。至于让应用程序等待我发现这个
还有
QThread.sleep()
(docs),具体取决于您想要什么样的效果。可能还值得查看 Qt docs 上的线程支持总体查找信息关于如何在 PyQt 中做事,我发现查看 Qt 文档然后用 Python 编写这些东西非常有用。十有八九这个方法有效。另一方面,PySide 可能也值得研究,它是另一个 python Qt 库。我以前没有使用过自己,因为之前已经在工作中,但我注意到他们已经发布了 1.0.6 版本。
更新
下面重申一下 Luke Woodward,您可以使用 QGraphicsScene 和 QGraphicsView 以面向对象的方式渲染内容。 QGraphicsScene 实际上并不渲染任何东西,它只是一个场景图,然后使用 QGraphicsView 来渲染场景图的内容。对于低级绘图,还有
QPainter
- 有一个基本绘图教程 此处。还值得一看QGraphicsItem
它是所有图形项目的基础和这里。要解释为什么有这么多不同的渲染方式,您必须询问其他人。我的两点意见是,这与 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.The above would be triggered on the
currentIndexChanged
event of an object calledconfSelectorCombo
. The setup of theconfSelectorCombo
is done in the GUI builder orQt 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:
The
QStyleSheet
andQStyle
act as proxies for changing the appearance of widgets (QStylesheet,QStyle).As for making the application wait I found this
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 docsOverall 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
andQGraphicsView
to render stuff in an object oriented way. TheQGraphicsScene
doesn't actually render anything it just a scene graph, theQGraphicsView
is then used to render the contents of the scene graph. For low level drawing there´s alsoQPainter
- there's a basic drawing tutorial here. It's also worth looking atQGraphicsItem
which is the base for all graphics items anddocs 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 onContext2D
andContext2DCanvas
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.