摘要:本文讲述一种基本的信号槽机制连接的语法,简单易懂非常适合新手。
1.PyQt5_PyCharm_软件设计和打包成安装包形式的exe程序
2.PyQt5_PyCharm_一种简单的PyQt5语法适合新手(本文)
3.PyQt5_PyCharm_软件设计和打包成安装包形式的exe程序
【1】基本的窗口代码。运行后就是一个窗口,窗口名字由window.setWindowTitle选择,窗口大小由window.resize选择,位置由window.move选择。
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("hello")
window.resize(1000, 600)
window.move(200, 200)
window.show()
sys.exit(app.exec_())
【2】添加控件,如按钮、标签等等。btn_1是自己取的名字,btn_1.move控制按钮的位置,btn_1.setText设置按钮上的名字,btn_1.setFont设置按钮上名字的字体、大小,btn_1.resize设置按钮的大小,btn_1.setStyleSheet是设置按钮的颜色、边框等等,这里border-radius等于resize的一半因此按钮为原型(按钮圆化弧度等于按钮长宽的一半)。
btn_1 = QPushButton(window)
btn_1.move(300, 400)
btn_1.setText("同意")
btn_1.setFont(QFont("Roman times", 20))
btn_1.resize(100, 100)
btn_1.setStyleSheet(u"background-color:rgb(170, 170, 255);"
"color:rgb(255,255,255);"
"border-radius:50px;"
"border:2px groove gray;"
"border-style: outset;"
)
btn_2 = QPushButton(window)
btn_2.move(600, 400)
btn_2.setText("反对")
btn_2.setFont(QFont("Roman times", 20))
btn_2.resize(100, 100)
btn_2.setStyleSheet(u"background-color:rgb(100, 150, 255);"
"color:rgb(255,255,255);"
"border-radius:50px;"
"border:2px groove gray;"
"border-style: outset;"
)
result_1 = QLabel(window)
result_1.setText("请投票")
result_1.setFont(QFont("Roman times", 60))
result_1.resize(1000, 100)
result_1.setStyleSheet(u"border: 0px Dashed;" "color:rgb(100,150,255);")
result_1.move(370, 100)
【3】设置信号槽的连接以及槽函数的定义编写,这里btn_1被点击的时候,会运行btn_1_clicked函数,而btn_1_clicked函数中QLabel.setText(result_1, "同意")以及下面的代码更改了标签中的字以及颜色。
btn_1.clicked.connect(lambda isChecked: btn_1_clicked())
btn_2.clicked.connect(lambda isChecked: btn_2_clicked())
def btn_1_clicked():
QLabel.setText(result_1, "同意")
QLabel.move(result_1, 420, 100)
QLabel.setStyleSheet(result_1, u"border: 0px Dashed;" "color:rgb(0,255,0);")
def btn_2_clicked():
QLabel.setText(result_1, "反对")
QLabel.move(result_1, 420, 100)
QLabel.setStyleSheet(result_1, u"border: 0px Dashed;" "color:rgb(255,0,0);")
【4】整体的运行就是点击同意按钮,标签变为同意,颜色变为绿色;点击反对按钮,标签变为反对,颜色变为红色。
from PyQt5.Qt import *
import sys
def btn_1_clicked():
QLabel.setText(result_1, "同意")
QLabel.move(result_1, 420, 100)
QLabel.setStyleSheet(result_1, u"border: 0px Dashed;" "color:rgb(0,255,0);")
def btn_2_clicked():
QLabel.setText(result_1, "反对")
QLabel.move(result_1, 420, 100)
QLabel.setStyleSheet(result_1, u"border: 0px Dashed;" "color:rgb(255,0,0);")
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle("投票系统")
window.resize(1000, 600)
window.move(200, 200)
btn_1 = QPushButton(window)
btn_1.move(300, 400)
btn_1.setText("同意")
btn_1.setFont(QFont("Roman times", 20))
btn_1.resize(100, 100)
btn_1.setStyleSheet(u"background-color:rgb(170, 170, 255);"
"color:rgb(255,255,255);"
"border-radius:50px;"
"border:2px groove gray;"
"border-style: outset;"
)
btn_2 = QPushButton(window)
btn_2.move(600, 400)
btn_2.setText("反对")
btn_2.setFont(QFont("Roman times", 20))
btn_2.resize(100, 100)
btn_2.setStyleSheet(u"background-color:rgb(100, 150, 255);"
"color:rgb(255,255,255);"
"border-radius:50px;"
"border:2px groove gray;"
"border-style: outset;"
)
result_1 = QLabel(window)
result_1.setText("请投票")
result_1.setFont(QFont("Roman times", 60))
result_1.resize(1000, 100)
result_1.setStyleSheet(u"border: 0px Dashed;" "color:rgb(100,150,255);")
result_1.move(370, 100)
btn_1.clicked.connect(lambda isChecked: btn_1_clicked())
btn_2.clicked.connect(lambda isChecked: btn_2_clicked())
window.show()
sys.exit(app.exec_())
【5】设计更好看UI界面