pyQt5 helloworld


1.  Qt designer 中界面设计

python3.6 与 Qt5.9

(1)new 一个Main Window

pyQt5 helloworld_第1张图片pyQt5 helloworld_第2张图片

(2)添加2个简单控件

pyQt5 helloworld_第3张图片

(3)设置、建立控件的信号槽关系

1 若两个控件是直接关联的时候,可以将信号槽箭头指向直接联系起来;

2 若不直接关联的时候,箭头拖到main window即可;

pyQt5 helloworld_第4张图片

添加触发函数,如上图中添加的是push button按下后的触发事件 clicked()

(4)添加响应函数

响应函数在触发函数激活后,通过信号槽传达给执行控件运行

如上图中添加的是名为hello()的响应函数。

响应函数可以在main()函数所在的类中调用

pyQt5 helloworld_第5张图片

(5)UI文件转换为py文件

Qt designer生成的文件后缀为ui,如:  xx.ui,需要转化为py文件在ide中引用。

在ui文件所在目录,shift+右键,在当前路径打开shell,执行:

pyuic5 xx.ui -o xx.py

运行后命令行后,会在同目录下生成xx.py文件,以供在main中引用

如果找不到pyuic5可执行程序,检查一下环境变量是否正确


2. main函数

通过main函数,实例化一个mian window类。

注意main函数中hello函数添加的位置

from PyQt5 import QtWidgets
from xx import Ui_MainWindow


class MyWindow(QtWidgets.QMainWindow,Ui_MainWindow):
    def __init__(self):
        super(MyWindow,self).__init__()
        self.setupUi(self)

    def hello(self):
        self.textEdit.setText("hello world")

if __name__=="__main__":
    import sys
    
    app=QtWidgets.QApplication(sys.argv)
    myshow=MyWindow()
    myshow.show()
    sys.exit(app.exec_())

运行结果

pyQt5 helloworld_第6张图片 pyQt5 helloworld_第7张图片

点击push button后,显示hello world。


你可能感兴趣的:(pyQt5)