您的位置:首页 > 编程语言 > Qt开发

【PyQt5】学习笔记(1)

2016-06-23 16:32 471 查看
# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets,QtCore                        #从pyqt库导入QtWindget通用窗口类
from formnew import Ui_Form

class mywindow(QtWidgets.QWidget,Ui_Form):                  #自己建的类,继承QtWidgets.Qwidget类方法和Ui_Form界面类

_signal = QtCore.pyqtSignal(str)                #定义信号,定义参数类型为str

def __init__(self):
super(mywindow,self).__init__()             #首先找到子类(mywindow)的父类(QWidget),然后把my的对象self转成QWidget的对象,然后被转化的self调用自己的init函数
self.setupUi(self)                          #直接继承界面类,调用类的setupUi方法

self.pushButton_2.clicked.connect(self.myPrint)       #连接自己的槽函数
self._signal.connect(self.mySignal)               #将信号连接到函数mySignal

def myPrint(self):                                     #自定义的槽函数。槽其实就是个函数(方法)
self.textBrowser.setText("")
self.textBrowser.append("我是槽函数")
self._signal.emit("发射信号,传递字符串")

def mySignal(self,string):                                  #自定义信号函数
self.textBrowser.append(string)                                           #接受到字符串,打印出来
self.textBrowser.append("我是信号函数")

if __name__=="__main__":
import sys

app=QtWidgets.QApplication(sys.argv)            #pyqt窗口必须在QApplication方法中使用
myshow=mywindow()                               #生成mywindow类的实例 myshow
myshow.show()                                   #myshow调用show方法
sys.exit(app.exec())                            #消息结束的时候,结束进程,并返回0,接着调用sys.exit(0)退出程序


上面这个是index.py

从qtdesigner直接生成的代码:

__author__ = 'yangyang5'

from PyQt5 import QtCore, QtGui, QtWidgets                              #导入模块

class Ui_Form(object):                                                  #创建窗口类,继承object

def setupUi(self, Form):
Form.setObjectName("Form")                                        #设置窗口名
Form.resize(400, 300)                                              #设置窗口大小
self.pushButton = QtWidgets.QPushButton(Form)                       #新建按钮,并加入到窗口中
self.pushButton.setGeometry(QtCore.QRect(270, 240, 75, 23))         #设置按钮的大小和位置
self.pushButton.setObjectName("pushButton")                         #设置按钮名
self.textBrowser = QtWidgets.QTextBrowser(Form)
self.textBrowser.setGeometry(QtCore.QRect(60, 20, 256, 192))
self.textBrowser.setObjectName("textBrowser")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(40, 240, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")

self.retranslateUi(Form)
self.pushButton.clicked.connect(Form.close)                         #点击按钮,关闭窗体
QtCore.QMetaObject.connectSlotsByName(Form)                         #关联信号槽

def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))                     #设置窗口标题
self.pushButton.setText(_translate("Form", "Quit"))                 #设置按钮名
self.pushButton_2.setText(_translate("Form", "Print"))


使用环境:

Python3.4 + Pyqt5第三方库

相关资料:
http://blog.csdn.net/a359680405/article/details/45096185
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: