仅记录效果和代码,部分功能待完善
界面
代码
import datetime
import random
import socket
import sys
import time
from PyQt5.QtCore import QCoreApplication, QObject, pyqtSignal, QTime, QTimer
from PyQt5.QtGui import QIcon, QFont, QColor
from PyQt5.QtWidgets import QApplication, QWidget, QToolTip, QPushButton, QMessageBox, QDesktopWidget, QAction, qApp, \
QMainWindow, QLCDNumber, QSlider, QVBoxLayout, QInputDialog, QLineEdit, QColorDialog, QFrame, QFontDialog, QLabel, \
QSizePolicy, QFileDialog, QTextEdit, QHBoxLayout, QSplitter, QTextBrowser, QComboBox, QTableWidget, QRadioButton
from PyQt5.QtCore import Qt
class Communicate(QObject):
closeApp = pyqtSignal()
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 主程序窗口
self.setGeometry(0, 0, 800, 600)
self.setWindowTitle('QT Chellenge')
self.center()
self.setWindowIcon(QIcon('images/manage.png'))
# 状态栏
self.statusBar()
# 工具栏
self.toolbar = self.addToolBar('退出')
# 创建一个菜单栏
menubar = self.menuBar()
# 主窗口布局
main_layout = QHBoxLayout()
widget = QWidget()
widget.setStyleSheet('QWidget { background-color: %s }' % QColor(150, 130, 20).name())
widget.setLayout(main_layout)
self.setCentralWidget(widget)
splitter1 = QSplitter(Qt.Horizontal)
main_layout.addWidget(splitter1)
self.lf = QFrame()
self.lf.setStyleSheet('QWidget { background-color: %s }' % QColor(110, 90, 60).name())
rf = QFrame()
rf.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
splitter1.addWidget(self.lf)
splitter1.addWidget(rf)
vbox = QVBoxLayout()
vbox2 = QVBoxLayout()
self.lf.setLayout(vbox)
rf.setLayout(vbox2)
setAction = QAction(QIcon('images/set.png'), '&Set', self)
setAction.setShortcut('Ctrl+1')
setAction.setStatusTip('Settings')
setAction.triggered.connect(qApp.quit)
exitAction = QAction(QIcon('images/exit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(qApp.quit)
downloadAction = QAction(QIcon('images/download.png'), '&Download', self)
downloadAction.setShortcut('Ctrl+D')
downloadAction.setStatusTip('Download file')
downloadAction.triggered.connect(qApp.quit)
runAction = QAction(QIcon('images/run.png'), '&Run', self)
runAction.setShortcut('Ctrl+R')
runAction.setStatusTip('Run service')
runAction.triggered.connect(qApp.quit)
stopAction = QAction(QIcon('images/stop.png'), '&Stop', self)
stopAction.setShortcut('Ctrl+T')
stopAction.setStatusTip('Stop service')
stopAction.triggered.connect(qApp.quit)
self.toolbar.addAction(setAction)
self.toolbar.addAction(downloadAction)
self.toolbar.addAction(runAction)
self.toolbar.addAction(stopAction)
self.toolbar.addAction(exitAction)
# endregion
# 添加菜单
fileMenu = menubar.addMenu('&File')
editMenu = menubar.addMenu('&Edit')
toolMenu = menubar.addMenu('&Tool')
helpMenu = menubar.addMenu('&Help')
exitMenu = menubar.addMenu('&Exit')
# 事件
openFile = QAction(QIcon('F:\Python\PyQt5\Dialogs\images\open.png'), '打开', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('打开一个文件')
openFile.triggered.connect(self.file_dia)
# 添加事件
fileMenu.addAction(openFile)
exitMenu.addAction(exitAction)
# 设置了提示框的字体
QToolTip.setFont(QFont('SansSerif', 10))
self.setToolTip('This is a <b>QWidget</b> widget')
btn = QPushButton('Button', self)
btn.setToolTip('This is a <b>QPushButton</b> widget')
btn.resize(btn.sizeHint())
table = QTableWidget(self)
btn_lay = QHBoxLayout(self)
vbox.addLayout(btn_lay)
btn_lay.addWidget(btn)
# 点击触发
btn1 = QPushButton('按钮一', self)
btn_lay.addWidget(btn1)
btn2 = QPushButton('按钮二', self)
btn_lay.addWidget(btn2)
btn1.clicked.connect(self.start)
btn2.clicked.connect(self.end)
# 创建信号连接到close槽
self.c = Communicate()
self.c.closeApp.connect(self.close)
self.btn = QPushButton('对话框', self)
btn_lay.addWidget(self.btn)
self.btn.clicked.connect(self.showDialog)
self.le = QLineEdit(self)
vbox.addWidget(self.le)
self.btn1 = QPushButton('color', self)
btn_lay.addWidget(self.btn1)
self.btn1.clicked.connect(self.chose_color)
com1 = QComboBox(self)
com1.addItems(["我的世界你曾经来过", "信息", "警告", "错误"])
vbox.addWidget(com1)
vbox.addWidget(table)
btn_f = QPushButton('字体', self)
btn_f.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
btn_lay.addWidget(btn_f)
btn_f.clicked.connect(self.chose_font)
self.textEdit = QTextEdit()
vbox.addWidget(self.textEdit)
tool = QHBoxLayout(self)
vbox2.addLayout(tool)
lab = QLabel("日志")
tool.addWidget(lab)
self.bt1 = QRadioButton("全部")
self.bt2 = QRadioButton("信息")
self.bt3 = QRadioButton("警告")
self.bt4 = QRadioButton("错误")
self.bt1.toggled.connect(self.clas_)
self.bt2.toggled.connect(self.clas_)
self.bt3.toggled.connect(self.clas_)
self.bt4.toggled.connect(self.clas_)
self.bt1.setStyleSheet('QWidget { background-color: %s }' % QColor(10, 200, 10).name())
tool.addWidget(self.bt1)
tool.addWidget(self.bt2)
tool.addWidget(self.bt3)
tool.addWidget(self.bt4)
self.browser = QTextBrowser(self) # shuchu
vbox2.addWidget(self.browser)
self.timer = QTimer(self)
self.timer.timeout.connect(self.test_log)
self.show()
def file_dia(self):
fname = QFileDialog.getOpenFileName(self, '打开文件', '/')
if fname[0]:
f = open(fname[0], 'r')
with f:
data = f.read()
self.textEdit.setText(str(data))
def chose_color(self):
"""颜色对话框"""
col = QColorDialog.getColor()
if col.isValid():
self.lf.setStyleSheet('QWidget { background-color: %s }' % col.name())
def chose_font(self):
"""字体对话框"""
font, ok = QFontDialog.getFont()
if ok:
self.com1.setFont(font)
def clas_(self):
send = self.sender()
if send.text() == "全部":
self.bt1.setStyleSheet('QWidget { background-color: %s }' % QColor(10, 200, 10).name())
self.bt2.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
self.bt3.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
self.bt4.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
elif send.text() == "信息":
self.bt1.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
self.bt2.setStyleSheet('QWidget { background-color: %s }' % QColor(10, 200, 10).name())
self.bt3.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
self.bt4.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
elif send.text() == "警告":
self.bt1.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
self.bt2.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
self.bt3.setStyleSheet('QWidget { background-color: %s }' % QColor(10, 200, 10).name())
self.bt4.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
else:
self.bt1.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
self.bt2.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
self.bt3.setStyleSheet('QWidget { background-color: %s }' % QColor(255, 255, 255).name())
self.bt4.setStyleSheet('QWidget { background-color: %s }' % QColor(10, 200, 10).name())
def showDialog(self):
"""输入对话框"""
text, ok = QInputDialog.getText(self, '输入对话框', '随意写点啥:')
if ok:
self.le.setText(str(text))
def mousePressEvent(self, event):
"""点击发射信号"""
self.c.closeApp.emit()
def browser(self):
"""点击发射信号"""
txt = self.le.text()
if not txt:
txt = "未输入"
self.browser.append(txt)
def start(self):
self.timer.start(2000) # 5000 单位是毫秒, 即 5 秒
self.statusBar().showMessage("即将测试")
def end(self):
self.statusBar().showMessage("测试停止")
self.timer.stop()
def test_log(self):
self.statusBar().showMessage("测试中。。。")
self.timer.stop()
m = ["info occured", "error occured", "warning occured", "nothing occured"]
t = datetime.datetime.now()
msg = f"{t}:" + random.choice(m)
sender = self.sender()
if sender.isChecked():
if sender.text() == "全部":
self.browser.append(msg)
elif sender.text() == "信息":
if msg.count("info") > 0:
self.browser.append(msg)
elif sender.text() == "警告":
if msg.count("warning") > 0:
self.browser.append(msg)
else:
if msg.count("error") > 0:
self.browser.append(msg)
self.timer.start()
def buttonClicked(self):
"""点击事件刷新状态栏"""
sender = self.sender()
self.statusBar().showMessage(sender.text() + ' 被按下')
def keyPressEvent(self, e):
"""esc退出"""
if e.key() == Qt.Key_Escape:
self.close()
def closeEvent(self, event):
"""关闭提示"""
reply = QMessageBox.question(self, 'Message',
"Are you sure to quit?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def center(self):
"""窗口居中"""
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def count_time(self): # todo
"""计时器"""
if self.flag == 1:
self.s = self.s + 1
if (self.s == 60):
self.s = 0
self.m = self.m + 1
if (self.m == 60):
self.m = 0
self.h = self.h + 1
'''创建时间字符串'''
time = QTime(self.h, self.m, self.s)
text = time.toString('hh:mm')
'''打印时间(每隔一秒中间的' : '号会闪一下'''
if time.second() % 2 == 0:
text = text[:2] + ' ' + text[3:]
# print(text)
self.lcd.display(text)
def show_time(self):
time_now = time.localtime()
self.lcd.display(time_now)
【参考】:
1.https://maicss.gitbooks.io/pyqt5/content/