Какого-то заготовленного кода нету. Суть в том, что, при выполнение Python скрипта происходит проверка текстового файла на наличие определённых условий и выводит результат в консоль с помощью print()
:
Как можно постоянно выводить текст из консоли в виджет Pyqt5?
code.py:
from datetime import datetime
from colorama import Fore, Style, init
import time, os, re
import csv, asyncio
import colorama
from valve.source.a2s import ServerQuerier, NoResponseError
import yaml
import config
pdeath = '.*?Got character ZDOID from (\w+) : 0:0'
sevent = '.*?Got character ZDOID from (\\w+)\\b(?!:)'
pkill = '.*? (\\w+):(\\w+)\\b(?!:)'
revent = '.*? (\w+):(\w+):(\w+)'
log = config.file
dlog = config.players_log
rlog = config.rewards_log
path_player = 'csv/playerstats.csv'
path_death = 'csv/deathlog.csv'
path_killed = 'csv/killedlog.csv'
path_reward = 'csv/rewardslog.csv'
colorama.init()
async def timenow():
now = datetime.now()
gettime = now.strftime("%d/%m/%Y %H:%M:%S")
return gettime
async def writecsv():
while True:
try:
with ServerQuerier(config.SERVER_ADDRESS) as server:
with open('csv/playerstats.csv', 'a', newline='') as f:
csvup = csv.writer(f, delimiter=',')
curtime, players = await timenow(), server.info()['player_count']
csvup.writerow([curtime, players])
print(f"{Fore.MAGENTA}{curtime} > Player count: {players}{Style.RESET_ALL}") ############################
except NoResponseError:
with open('csv/playerstats.csv', 'a', newline='') as f:
csvup = csv.writer(f, delimiter=',')
curtime, players = await timenow(), '0'
csvup.writerow([curtime, players])
print(Fore.RED + curtime, 'Unable to connect to server' + Style.RESET_ALL) ############################
await asyncio.sleep(60)
async def deathcount():
while True:
with open(log, encoding='utf-8', mode='r') as f:
f.seek(0,2)
while True:
line = f.readline()
if(re.search(pdeath, line)):
pname = re.search(pdeath, line).group(1)
curtime = await timenow()
with open('csv/deathlog.csv', 'a', newline='', encoding='utf-8') as dl:
deathup = csv.writer(dl, delimiter=',')
deathup.writerow([curtime, pname])
print(Fore.CYAN + f"{curtime} > {pname} умер!" + Style.RESET_ALL) ############################
await asyncio.sleep(0.2)
async def joinedcount():
while True:
with open(log, encoding='utf-8', mode='r') as f:
f.seek(0,2)
while True:
line = f.readline()
first_try = '.*? User (\w+) connected.'
second_try = '.*? User (\w+)\s(\w+) connected.'
curtime = await timenow()
if(re.search(first_try, line)):
player = re.search(first_try, line).group(1)
with open('csv/joinedcount.csv', 'a', newline='', encoding='utf-8') as j:
joinedup = csv.writer(j, delimiter=',')
joinedup.writerow([curtime, player])
print(Fore.LIGHTGREEN_EX + f"{curtime} > {Fore.WHITE}{player}{Fore.LIGHTGREEN_EX} присоединился на сервер! {Style.RESET_ALL}") ############################
if(re.search(second_try, line)):
player = re.search(second_try, line).group(1), re.search(second_try, line).group(2)
player = f"{player[0]} {player[1]}"
with open('csv/joinedcount.csv', 'a', newline='', encoding='utf-8') as j:
joinedup = csv.writer(j, delimiter=',')
joinedup.writerow([curtime, player])
print(Fore.LIGHTGREEN_EX + f"{curtime} > {Fore.WHITE}{player}{Fore.LIGHTGREEN_EX} присоединился на сервер! {Style.RESET_ALL}") ############################
forma.py
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1186, 722)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.background = QtWidgets.QLabel(self.centralwidget)
self.background.setGeometry(QtCore.QRect(-20, -180, 1321, 941))
self.background.setStyleSheet("background-color: rgb(18, 18, 18);")
self.background.setText("")
self.background.setObjectName("background")
self.PVELogs = QtWidgets.QPlainTextEdit(self.centralwidget)
self.PVELogs.setGeometry(QtCore.QRect(80, 50, 491, 491))
font = QtGui.QFont()
font.setFamily("Fira Code")
font.setPointSize(9)
self.PVELogs.setFont(font)
self.PVELogs.setStyleSheet("background-color: rgb(30, 30, 30);color: rgb(255, 255, 255);")
self.PVELogs.setReadOnly(True)
self.PVELogs.setObjectName("PVELogs")
self.PVPLogs = QtWidgets.QPlainTextEdit(self.centralwidget)
self.PVPLogs.setGeometry(QtCore.QRect(620, 50, 491, 491))
font = QtGui.QFont()
font.setFamily("Fira Code")
font.setPointSize(9)
self.PVPLogs.setFont(font)
self.PVPLogs.setStyleSheet("background-color: rgb(30, 30, 30);color: rgb(255, 255, 255);")
self.PVPLogs.setReadOnly(True)
self.PVPLogs.setObjectName("PVPLogs")
self.PVELabel = QtWidgets.QLabel(self.centralwidget)
self.PVELabel.setGeometry(QtCore.QRect(80, 20, 211, 21))
font = QtGui.QFont()
font.setFamily("Consolas")
font.setPointSize(10)
font.setBold(True)
font.setItalic(False)
font.setWeight(75)
self.PVELabel.setFont(font)
self.PVELabel.setStyleSheet("color: rgb(255, 255, 255);")
self.PVELabel.setObjectName("PVELabel")
self.PVPLabel = QtWidgets.QLabel(self.centralwidget)
self.PVPLabel.setGeometry(QtCore.QRect(620, 20, 211, 21))
font = QtGui.QFont()
font.setFamily("Consolas")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.PVPLabel.setFont(font)
self.PVPLabel.setStyleSheet("color: rgb(255, 255, 255);")
self.PVPLabel.setObjectName("PVPLabel")
self.settings = QtWidgets.QPushButton(self.centralwidget)
self.settings.setGeometry(QtCore.QRect(10, 690, 75, 23))
self.settings.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
self.settings.setObjectName("settings")
self.offPVE = QtWidgets.QPushButton(self.centralwidget)
self.offPVE.setGeometry(QtCore.QRect(230, 550, 121, 31))
font = QtGui.QFont()
font.setFamily("Arial")
font.setBold(True)
font.setWeight(75)
self.offPVE.setFont(font)
self.offPVE.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
self.offPVE.setObjectName("offPVE")
self.onPVE = QtWidgets.QPushButton(self.centralwidget)
self.onPVE.setGeometry(QtCore.QRect(90, 550, 121, 31))
font = QtGui.QFont()
font.setFamily("Arial")
font.setBold(True)
font.setWeight(75)
self.onPVE.setFont(font)
self.onPVE.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
self.onPVE.setObjectName("onPVE")
self.onPVP = QtWidgets.QPushButton(self.centralwidget)
self.onPVP.setGeometry(QtCore.QRect(630, 550, 121, 31))
font = QtGui.QFont()
font.setFamily("Arial")
font.setBold(True)
font.setWeight(75)
self.onPVP.setFont(font)
self.onPVP.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
self.onPVP.setObjectName("onPVP")
self.offPVP = QtWidgets.QPushButton(self.centralwidget)
self.offPVP.setGeometry(QtCore.QRect(770, 550, 121, 31))
font = QtGui.QFont()
font.setFamily("Arial")
font.setBold(True)
font.setWeight(75)
self.offPVP.setFont(font)
self.offPVP.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
self.offPVP.setObjectName("offPVP")
self.timelaunch = QtWidgets.QLabel(self.centralwidget)
self.timelaunch.setGeometry(QtCore.QRect(810, 690, 211, 21))
font = QtGui.QFont()
font.setBold(False)
font.setItalic(False)
font.setWeight(50)
font.setStrikeOut(False)
self.timelaunch.setFont(font)
self.timelaunch.setStyleSheet("color: rgb(255, 255, 255); background-color: rgb(18, 18, 18);")
self.timelaunch.setObjectName("timelaunch")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "VALHEIMBY BOT - SERVERS LOG"))
self.PVELogs.setPlainText(_translate("MainWindow", "QPlainTextEdit"))
self.PVPLogs.setPlainText(_translate("MainWindow", "QPlainTextEdit"))
self.PVELabel.setText(_translate("MainWindow", "PVE Logs:"))
self.PVPLabel.setText(_translate("MainWindow", "PVP Logs:"))
self.settings.setText(_translate("MainWindow", "Настройки"))
self.offPVE.setText(_translate("MainWindow", "Завершить PVE.py"))
self.onPVE.setText(_translate("MainWindow", "Запустить PVE.py"))
self.onPVP.setText(_translate("MainWindow", "Запустить PVP.py"))
self.offPVP.setText(_translate("MainWindow", "Завершить PVP.py"))
self.timelaunch.setText(_translate("MainWindow", "Время с момента запуска: {time_launch}"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
QTextEditLogger
, вот так можно сделать? pastebin.com/iSLeYRjv