pyqt 中的你好世界? [英] Hello world in pyqt?

查看:37
本文介绍了pyqt 中的你好世界?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在使用 pycharm 开发 python web 应用程序.我想用 QT 框架开发桌面应用程序.我已经安装了pyqt.我在 pyqt 中搜索了 hello world 并找到了这个:

Currently I'm using pycharm to develop python web application. I want to develop desktop application with QT framework. I've installed pyqt. I've searched about hello world in pyqt and find this:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.button = QtGui.QPushButton('Test', self)
        self.button.clicked.connect(self.handleButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)

    def handleButton(self):
        print ('Hello World')

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

但是我不知道把这段代码放在哪里?这是我的 pyqt 设计师的样子:

But I don't know where to put this code? this is my pyqt designer looks like:

是否可以告诉我在哪里编写代码以及如何处理按钮点击?

Is it possible to tell me where to write code and how to handle a button click?

推荐答案

看起来您发布的代码是从这个答案复制的 我的.该代码是一个简单的手写示例,根本不涉及使用 Qt Designer.

It looks like the code you posted was copied from this answer of mine. That code is a simple, hand-written example that doesn't involve using Qt Designer at all.

使用 Qt 设计器的Hello World"示例将从一个 ui 文件开始,如下所示:

A "Hello World" example using Qt Designer would start off with a ui file like this:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Window</class>
 <widget class="QWidget" name="Window">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>171</width>
    <height>61</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Hello World</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QPushButton" name="button">
     <property name="text">
      <string>Test</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

这个文件可以保存为helloworld.ui并在Qt Designer中打开.

This file can be saved as helloworld.ui and opened in Qt Designer.

首先要了解 Qt Designer,它不是 IDE - 它仅用于设计 GUI,而不是主要程序逻辑.程序逻辑单独编写,然后连接到GUI.

The first thing to understand about Qt Designer, is that it is not an IDE - it is only used for designing the GUI, not the main program logic. The program logic is written separately and connected to the GUI afterwards.

有两种方法可以做到这一点.第一种是直接加载 ui 文件,使用 uic 模块:

There are two ways to do this. The first is to load the ui file directly, using the uic module:

import sys, os
from PyQt4 import QtGui, QtCore, uic

DIRPATH = os.path.join(os.path.dirname(os.path.abspath(__file__)))

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        uic.loadUi(os.path.join(DIRPATH, 'helloworld.ui'), self)
        self.button.clicked.connect(self.handleButton)

    def handleButton(self):
        print('Hello World')

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

这会将 GUI 注入本地 Window 类,该类是与 Qt Designer 的顶级 GUI 类匹配的子类(在本例中也称为Window",但可以是任何你喜欢).其他 GUI 小部件成为子类的属性 - 因此 QPushButton 可用作 self.button.

This injects the GUI into the local Window class, which is a subclass matching the top-level GUI class from Qt Designer (which in this case is also called "Window", but can be anything you like). The other GUI widgets become attributes of the subclass - so the QPushButton is available as self.button.

将 GUI 与程序逻辑连接的另一种方法是使用 pyuic 工具ui文件生成python模块:

The other way to connect the GUI with the program logic, is to use the pyuic tool to generate a python module from the ui file:

pyuic4 --output=helloworld.py helloworld.ui

然后可以导入到主应用程序中:

which can then be be imported into the main application:

import sys
from PyQt4 import QtGui, QtCore
from helloworld import Ui_Window

class Window(QtGui.QWidget, Ui_Window):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.setupUi(self)
        self.button.clicked.connect(self.handleButton)

    def handleButton(self):
        print('Hello World')

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

setupUi 方法继承自生成的 Ui_Window 类,与 uic.loadUi 的作用完全相同.

The setupUi method is inherited from the generated Ui_Window class, and does exactly the same thing as uic.loadUi.

这篇关于pyqt 中的你好世界?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆