视频教程:https://www.bilibili.com/video/BV1US4y1P7ae
pip install PyQt6 -i https://pypi.douban.com/simple
QMainWindow-主程序窗口基类
QDialog-对话窗口基类
QWidget-用户界面对象基类
写代码当然很慢了,拖控件多方便。
这个现在 PyQt6 默认安装好了,如果没有再安装。
安装:pip install pyqt6-tools -i https://pypi.douban.com/simple
打开位置:...\anaconda\Lib\site-packages\qt6_applications\Qt\bin\designer.exe
ui 文件转换为 py 文件:在项目路径下:pyuic6 -x WindowUI.ui -o windowui.py
,文件名自定。
加载 ui 文件:
from PyQt6.QtWidgets import QApplication, QWidget
import sys
from PyQt6 import uic
class UI(QWidget):
def __init__(self):
super().__init__()
uic.loadUi("WindowUI.ui", self)
app = QApplication(sys.argv)
window = UI()
window.show()
app.exec()
from PyQt6.QtWidgets import QApplication, QWidget, QLabel
from PyQt6.QtGui import QIcon, QFont, QPixmap, QMovie
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 700, 400)
self.setWindowTitle("Python GUI")
self.setWindowIcon(QIcon('image/logo.png')) # 程序左上角图标
# self.setFixedWidth(700)
# self.setFixedHeight(400) # 固定窗口大小,不鞥改变
# self.setStyleSheet('background-color:green') # 窗口颜色
# self.setWindowOpacity(0.5) # 窗口不透明度 0-1
'''
label = QLabel("python GUI", self)
label.setText("new text is here")
label.move(100, 100)
label.setFont(QFont("Sanseif", 15))
label.setStyleSheet('color:red')
# label.setText(str(12))
label.setNum(15)
label.clear()
'''
label = QLabel(self)
pixmap = QPixmap('image/zy.jpg') # 静态图
label.setPixmap(pixmap)
movie = QMovie('image/xx.gif') # gif
movie.setSpeed(500)
label.setMovie(movie)
movie.start()
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMenu
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 700, 400)
self.setWindowTitle("Python GUI")
self.setWindowIcon(QIcon('image/logo.png')) # 程序左上角图标
self.create_button()
def create_button(self):
btn = QPushButton('click', self)
btn.setGeometry(100, 100, 200, 200)
btn.setFont(QFont('Times',14,QFont.Weight.ExtraBold))
btn.setIcon(QIcon('image/logo.png'))
btn.setIconSize(QSize(36, 36))
# popup menu
menu = QMenu()
menu.setFont(QFont('Times', 14, QFont.Weight.ExtraBold))
menu.setStyleSheet('background-color:green')
menu.addAction('Copy')
menu.addAction('Cut')
menu.addAction('Paste')
btn.setMenu(menu)
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
from PyQt6.QtWidgets import QApplication, QWidget, QLineEdit
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 700, 400)
self.setWindowTitle("Python GUI")
self.setWindowIcon(QIcon('image/logo.png')) # 程序左上角图标
line_edit = QLineEdit(self)
line_edit.setFont(QFont('Sanserif', 15))
# line_edit.setText('请在此输入:') # 在框内显性显示
# line_edit.setPlaceholderText('请输入用户名') # 在框内隐性显示
# line_edit.setEnabled(False) # 禁用编辑
line_edit.setEchoMode(QLineEdit.EchoMode.Password) # 设置为密码mode
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
from PyQt6.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 700, 400)
self.setWindowTitle("Python GUI")
self.setWindowIcon(QIcon('image/logo.png')) # 程序左上角图标
hbox = QHBoxLayout()
btn1 = QPushButton('click 1')
btn2 = QPushButton('click 2')
btn3 = QPushButton('click 3')
btn4 = QPushButton('click 4')
hbox.addWidget(btn1)
hbox.addWidget(btn2)
hbox.addWidget(btn3)
hbox.addWidget(btn4)
# hbox.setSpacing(100) # 设置间距
# hbox.setStretch(5) # 拉伸
self.setLayout(hbox) # 设置主窗口布局
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
同 QHBoxLayout,替换一下即可
from PyQt6.QtWidgets import QApplication, QWidget, QGridLayout, QPushButton
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 700, 400)
self.setWindowTitle("Python GUI")
self.setWindowIcon(QIcon('image/logo.png')) # 程序左上角图标
grid = QGridLayout()
btn1 = QPushButton('click 1')
btn2 = QPushButton('click 2')
btn3 = QPushButton('click 3')
btn4 = QPushButton('click 4')
grid.addWidget(btn1, 0, 0) # 设置行列号
grid.addWidget(btn2, 0, 1)
grid.addWidget(btn3, 1, 0)
grid.addWidget(btn4, 1, 1)
self.setLayout(grid)
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QHBoxLayout
from PyQt6.QtGui import QIcon, QFont
from PyQt6.QtCore import QSize
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(200, 200, 700, 400)
self.setWindowTitle("Python GUI")
self.setWindowIcon(QIcon('image/logo.png')) # 程序左上角图标
self.create_widget()
def create_widget(self):
hbox = QHBoxLayout()
btn = QPushButton('change text')
btn.clicked.connect(lambda: self.clicked_btn())
self.label = QLabel('六级不过是狗')
hbox.addWidget(btn)
hbox.addWidget(self.label)
self.setLayout(hbox)
def clicked_btn(self):
self.label.setText('我是狗')
self.label.setFont(QFont('Times', 15))
self.label.setStyleSheet('color:red')
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec())