import base64
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QRect, Qt
from PyQt5.QtGui import QFont,QPixmap, QImage
from PyQt5.QtWidgets import QApplication, QWidget, QFileDialog
from radio import Ui_Dialog as parentDialog
from A import Ui_Dialog as xxxxx
import cv2
import io
class myRadio(QWidget,parentDialog):
def __init__(self):
super().__init__()
self.setupUi(self)
#下面是单选事件的处理
self.radioButton.toggled.connect(lambda :self.dianji_danXuan(self.radioButton))
self.radioButton_2.toggled.connect(lambda: self.dianji_danXuan(self.radioButton_2))
self.radioButton_3.toggled.connect(lambda: self.dianji_danXuan(self.radioButton_3))
self.radioButton_7.toggled.connect(lambda: self.dianji_danXuan(self.radioButton_7))
self.radioButton_4.toggled.connect(lambda :self.dianji_danXuan(self.radioButton_4))
self.radioButton_5.toggled.connect(lambda: self.dianji_danXuan(self.radioButton_5))
self.radioButton_6.toggled.connect(lambda: self.dianji_danXuan(self.radioButton_6))
#水平滑动条
self.horizontalSlider.valueChanged.connect(self.valChange)
# 点击 显示图片按钮 打开图片选择对话框
self.openPicBtn.clicked.connect(self.openimage)
#多选框事件绑定
self.checkBox.stateChanged.connect(lambda :self.dianji_duoXuan(self.checkBox))
self.checkBox_2.stateChanged.connect(lambda :self.dianji_duoXuan(self.checkBox_2))
self.checkBox_3.stateChanged.connect(lambda :self.dianji_duoXuan(self.checkBox_3))
self.checkBox_4.stateChanged.connect(lambda :self.dianji_duoXuan(self.checkBox_4))
# 设置窗体的标题
self.setWindowTitle("单选demo")
#向下拉列表中添加一个item
self.comboBox.addItem("xushuo")
self.comboBox.currentIndexChanged.connect(lambda :self.dianji_xiaLa( self.comboBox))
print(self.comboBox.currentText()) #当前下拉框选中的文本
# 当前文字类型下拉框的文本
print(self.fontComboBox.currentText())
#下面是带背景的文本框
self.textBrowser.setFont(QFont("微软雅黑",12))
self.textBrowser.setText(
'''1. xuhaitao徐海涛淘还需徐海涛淘还需徐海涛淘还需徐海涛淘还需徐海涛淘还需徐海涛淘还需徐海涛淘还需
2. hunkxu
3. xudashuo
''')
self.imageLabel.setStyleSheet("border:1px solid red") #给label图片显示框添加一个边框样式
#默认开始时从项目路径下加载一张图片显示
self.localDisplayPic()
def localDisplayPic(self):
img = cv2.imread('qianzi.png')
height, width, bytesPerComponent = img.shape
bytesPerLine = 3 * width
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
QImg = QImage(img.data, width, height, bytesPerLine, QImage.Format_RGB888)
QImg= QImg.scaled(self.imageLabel.width(),self.imageLabel.height()) #使得整个图像拉伸铺满到Label标签
pixmap = QPixmap.fromImage(QImg)
self.imageLabel.setPixmap(pixmap)
self.imageLabel.setCursor(Qt.CrossCursor) #鼠标经过图片时,显示一个十字准星
def openimage(self):
imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "", "*.jpg;;*.png;;All Files(*)")
jpg = QtGui.QPixmap(imgName).scaled(self.imageLabel.width(), self.imageLabel.height()) #缩放以铺满整个图片
self.imageLabel.setPixmap(jpg)
#单选按钮的槽函数
def dianji_danXuan (self,anniu):
if anniu.objectName() =="radioButton" and anniu.isChecked():
print(anniu.text())
if anniu.objectName() =="radioButton_2" and anniu.isChecked():
print(anniu.text())
if anniu.objectName() =="radioButton_3" and anniu.isChecked():
print(anniu.text())
if anniu.objectName() == "radioButton_7" and anniu.isChecked():
print(anniu.text())
if anniu.objectName() == "radioButton_4" and anniu.isChecked():
print(anniu.text())
if anniu.objectName() == "radioButton_5" and anniu.isChecked():
print(anniu.text())
if anniu.objectName() == "radioButton_6" and anniu.isChecked():
print(anniu.text())
#多选按钮槽函数
def dianji_duoXuan (self,anniu):
if anniu.objectName() =="checkBox" :
if anniu.isChecked():
print(anniu.text())
else:
print(anniu.text()+" 取消勾选")
elif anniu.objectName() =="checkBox_2" :
if anniu.isChecked():
print(anniu.text())
else:
print(anniu.text()+" 取消勾选")
elif anniu.objectName() =="checkBox_3" :
if anniu.isChecked():
print(anniu.text())
else:
print(anniu.text()+" 取消勾选")
elif anniu.objectName() =="checkBox_4" :
if anniu.isChecked():
print(anniu.text())
else:
print(anniu.text()+" 取消勾选")
#下拉框槽函数
def dianji_xiaLa (self,anniu):
print(anniu.currentText())
def valChange(self):
self.label.setNum(self.horizontalSlider.value()) #更改文本的内容,不能用setText,否则会奔
self.label.setFont(QFont("微软雅黑",self.horizontalSlider.value())) #更改文本的字体字号
if __name__ == '__main__':
app=QApplication(sys.argv)
parent = myRadio()
# 显示
parent.show()
sys.exit(app.exec_())
运行结果:
FR:徐海涛(hunk Xu)
QQ技术交流群:386476712