Pyqt5之基础操作—1

简介

本文介绍了部分Pyqt5开发GUI的基础操作,适合有一定基础的朋友阅读,如果您想学习Pyqt图形界面开发,请 点击这里。.
由于本人能力有限,如文章有不足之处欢迎留言指正。

常用weight

名称 简述 使用方法
MainWindow 主窗口,可以承载menuBar、statusBar和其他Widget class(MainWindow)
QLabel 标签,起提示作用,可设置信号槽 label_1 = QLabel(‘我是标签’)
QLineEdit 单行文本输入框 lineEdit_1=QLineEdit()
QTextEdit 多行文本输入框 textEdit_1 = QTextEdit()
Button 按钮,通常和方法绑定,当点击时可执行对应方法 QLineEdit()
QComboBox 菜单栏 comboBox_1 = QComboBox()

基本格式

# 这些模块建议根据需求使用from *** import ***的形式,因为打包之后程序会很大
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class Window(QMainWindow):
    def __init__(self):
        super(Window,self).__init__()
        self.setupUi()

    def setupUi(self):
    	self.setWindowTitle('一个小GUI')  # 设置窗口标题
        self.resize(769, 336)  # 设置窗口大小
if __name__ == '__main__':
    app = QApplication(sys.argv)    # 创建程序
    window = Window()   # 创建窗口实例
    window.show()   # 显示窗口
    sys.exit(app.exec_())   # 安全退出

注意:为做到博客简洁,以下代码均放在Window类的setupUi方法下

为部件设置大小和位置

# 方法一
        self.button1 = QPushButton('我是一个小按钮', self)  #注意,不要丢掉self参数,否则屏幕不会显示按钮
        self.button1.setGeometry(200, 100, 50, 100)  # 座标为x=200,y=100,width=50,height=100
# 方法二
		self.button1 = QPushButton('我是一个小按钮', self)
		self.button1.move(200,100)  # 座标为x=200,y=100
		self.button1.resize(50, 100)  # width=50,height=100

为按钮添加单击事件

# 单个按钮
        self.button1 = QPushButton('我是一个小按钮', self)
        self.button1.setGeometry(200, 100, 50, 100)
        self.button1.clicked.connect(onClick_Button)  # 点击时会调用onClick_Button这个方法
	def onClick_Button(self):
		print("我被点击了")
		
# 多个按钮,可以选择一个按钮关联一个函数的方式,也可以通过判断按钮名称的方式来完成
        self.button1 = QPushButton('我是一个按钮A', self)
        self.button1.setGeometry(200, 100, 50, 100)
        self.button1.clicked.connect(lambda:self.onClick_Button(self.button1))  # button1是函数的实参
        self.button2 = QPushButton('我是一个按钮B', self)
        self.button2.setGeometry(300, 100, 50, 100)
        self.button2.clicked.connect(lambda:self.onClick_Button(self.button2))  # button2是函数的实参
	def onClick_Button(self,button):  # button是传入的按钮名称
		buttton_name=button.text()
		if buttton_name=='button1':
			print("button1被按下了")
		else:
			print("button2被按下了")

设置菜单选项

        self.comboBox1 = QComboBox(self)  # 实例化对象,创建一个菜单栏
        self.comboBox1.setGeometry(110,40,50,20)  # 设置位置和大小
        self.comboBox1.setObjectName("第一个菜单栏")  # 起一个方便记的名字
        self.comboBox1.addItems(["选项一","选项二","选项三"])  # 为菜单添加选项
        '''
        或者是使用addItem方法(少了个s):
        self.comboBox1.addItem("选项一")
        self.comboBox1.addItem("选项二")
        self.comboBox1.addItem("选项三")
        '''

给布局添加位置大小和部件

        self.groupBox1 = QGroupBox(self)  # 实例化对象
        self.groupBox1.setGeometry(200, 100, 141, 131)  # 设置位置和大小
        self.groupBox1.setObjectName("关于Y轴的")  # 添加一个方便自己辨别的名字
        self.groupBox1.setTitle("Y轴")  # 设置标题
        self.label1=QLabel("起始")
        ##########以上代码是为了实现布局的嵌套,当只有布局不需要嵌套则可以不写上述代码和下一行代码的groupBox1参数##########
        self.mainLayout = QGridLayout(self.groupBox1)  # 使用栅格化布局,groupBox1是创建的对象,当参数只有self时,默认在主窗口放置
        # self.mainLayout.setGeometry(150, 50, 50, 50)  # 设置位置和大小
        self.mainLayout.addWidget(self.label1, 0, 0,alignment=Qt.AlignCenter)  # 在第一行第一列放置,对齐方式为水平居中对齐

补充:对齐方式Qt.Alignment可能取值有

名称 对齐方式
Qt.AlignLeft 水平居左对齐
Qt.AlignRight 水平居右对齐
Qt.AlignCenter 水平居中对齐
Qt.AlignJustify 水平两端对齐
Qt.AlignTop 垂直靠上对齐
Qt.AlignBottom 垂直靠下对齐
Qt.AlignVCenter 垂直居中对齐

好啦,没事了

创建一个简单的水平布局

import sys
# QToolTip是提示信息的
from PyQt5.QtWidgets import QHBoxLayout,QMainWindow,QApplication,QToolTip,QPushButton,QWidget
from PyQt5.QtGui import QFont

class TooltipForm(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.button1 = QPushButton('我的按钮')
        layout = QHBoxLayout()  # 创建一个水平布局
        layout.addWidget(self.button1)  # 将button部件添加到水平布局中
		self.setLayout(layout)
		####上述代码已达到目的####

		##下面的代码是介绍怎样创建一个frame,不感兴趣可以不用看##
        mainFrame = QWidget()  # 在主窗口上再创建一个小窗口
        mainFrame.setLayout(layout)  # 将水平布局放到小窗口上
        self.setCentralWidget(mainFrame)  # 将小窗口放置到主窗口上

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = TooltipForm()
    window.show()
    sys.exit(app.exec_())

为部件创建提示信息

import sys
# QToolTip是提示信息的
from PyQt5.QtWidgets import QHBoxLayout,QMainWindow,QApplication,QToolTip,QPushButton,QWidget
from PyQt5.QtGui import QFont

class TooltipForm(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        QToolTip.setFont(QFont('Simsun',12))#设置提示信息的字体
        QToolTip.setFont(QFont('Simsun',12))  # 设置提示信息的字体
        self.setGeometry(300,30,200,300)  # 设置窗口的位置和大小
        self.setWindowTitle('设置控件提示消息')  # 设置窗口的标题
		self.setToolTip('今天是星期五')  # 把鼠标放到界面停留一会  就会显示提示信息
        # 支持html语法,也可以写成:self.setToolTip('今天是<b>星期五</b>')

        self.button1 = QPushButton('我的按钮')
        self.button1.setToolTip('这是一个按钮,Are you ok?')
        layout = QHBoxLayout()
        layout.addWidget(self.button1)

        mainFrame = QWidget()
        mainFrame.setLayout(layout)
        self.setCentralWidget(mainFrame)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = TooltipForm()
    window.show()
    sys.exit(app.exec_())

设置窗口图标和应用程序图标

窗口图标是打开GUI之后,左上角的图标,Mac系统没有这个属性
应用图标是打开GUI之后,下面任务栏的图标

import sys
from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5.QtGui import QIcon

class IconForm(QMainWindow):
    def __init__(self):
        super(IconForm,self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300,300,250,250)
        self.setWindowTitle('设置窗口图标')  # 设置主窗口的标题
        self.setWindowIcon(QIcon('./images/Basilisk.ico'))  # 设置窗口图标,参数是图片地址

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('./images/Dragon.ico'))  # 设置应用程序图标,参数是图片地址
    main = IconForm()
    main.show()
    sys.exit(app.exec_())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章