#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# PROJECT MADE WITH: Qt Designer and PySide6/Pycharm
# @Created on: 2023/4/20 14:19
# @Author: By Kevin
# @Filename: LogViewer.py
# @Version: 0.1.0
import sys
from PySide6.QtWidgets import QApplication, QMainWindow, QTextEdit, QFileDialog
from PySide6.QtGui import QAction
class LogViewer(QMainWindow):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
# Set up the main window
self.setWindowTitle('Log Viewer')
self.setGeometry(100, 100, 800, 600)
# Create a QTextEdit widget to display log contents
self.text_edit = QTextEdit(self)
self.setCentralWidget(self.text_edit)
# Set the QTextEdit style
self.text_edit.setStyleSheet("""
QTextEdit {
background-color: #2B2B2B;
color: #FB6A67;
font-size: 10pt;
}
""")
# Create a menu bar
menu_bar = self.menuBar()
file_menu = menu_bar.addMenu('File')
# Add "Open" action to the menu
open_action = QAction('Open', self)
open_action.triggered.connect(self.open_log)
file_menu.addAction(open_action)
# Add "Exit" action to the menu
exit_action = QAction('Exit', self)
exit_action.triggered.connect(self.close)
file_menu.addAction(exit_action)
def open_log(self):
options = QFileDialog.Options()
file_name, _ = QFileDialog.getOpenFileName(self, "Open Log File", "", "Log Files (*.log);;All Files (*)", options=options)
if file_name:
with open(file_name, 'r') as log_file:
log_contents = log_file.read()
self.text_edit.setPlainText(log_contents)
if __name__ == '__main__':
app = QApplication(sys.argv)
log_viewer = LogViewer()
log_viewer.show()
sys.exit(app.exec())
运行结果如下: