Делаю программу - лаунчер для игры. Работаю частично в Qt Deisgner и в редакторе. Есть Label, который выводит статус и кол. игроков на двух серверах.
def retranslateUi(self, MainWindow):
import requests
from bs4 import BeautifulSoup
import time
import webbrowser
URLServerOne = "https://www.battlemetrics.com/servers/valheim/10846961"
ResponseServerOne = requests.get(URLServerOne)
SoupServerOne = BeautifulSoup(ResponseServerOne.content, 'html.parser')
PlayerStatusServerOne = SoupServerOne.select_one('.dl-horizontal :nth-child(8)').text
PlayerCountServerOne = SoupServerOne.select_one('.dl-horizontal :nth-child(4)').text
URLServerTwo = "https://www.battlemetrics.com/servers/valheim/10648280"
ResponseServerTwo = requests.get(URLServerTwo)
SoupServerTwo = BeautifulSoup(ResponseServerTwo.content, 'html.parser')
PlayerStatusServerTwo = SoupServerTwo.select_one('.dl-horizontal :nth-child(8)').text
PlayerCountServerTwo = SoupServerTwo.select_one('.dl-horizontal :nth-child(4)').text
self.label.setText(_translate("MainWindow", "Статус серверов: Valheim.by #1 - {0} - {1}\nValheim.by #2 - {2} - {3}".format(PlayerStatusServerOne, PlayerCountServerOne, PlayerStatusServerTwo, PlayerCountServerTwo)))
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_())
Хочу сделать бесконечный цикл, который каждые 15 минут будет обновлять счётчик онлайн на сервере:
def retranslateUi(self, MainWindow):
import requests
from bs4 import BeautifulSoup
import time
import webbrowser
import threading
while True:
URLServerOne = "https://www.battlemetrics.com/servers/valheim/10846961"
ResponseServerOne = requests.get(URLServerOne)
SoupServerOne = BeautifulSoup(ResponseServerOne.content, 'html.parser')
PlayerStatusServerOne = SoupServerOne.select_one('.dl-horizontal :nth-child(8)').text
PlayerCountServerOne = SoupServerOne.select_one('.dl-horizontal :nth-child(4)').text
URLServerTwo = "https://www.battlemetrics.com/servers/valheim/10648280"
ResponseServerTwo = requests.get(URLServerTwo)
SoupServerTwo = BeautifulSoup(ResponseServerTwo.content, 'html.parser')
PlayerStatusServerTwo = SoupServerTwo.select_one('.dl-horizontal :nth-child(8)').text
PlayerCountServerTwo = SoupServerTwo.select_one('.dl-horizontal :nth-child(4)').text
if PlayerStatusServerOne == "online":
PlayerStatusServerOne = "Онлайн"
if PlayerStatusServerOne == "offline":
PlayerStatusServerOne = "Оффлайн"
if PlayerStatusServerTwo == "online":
PlayerStatusServerTwo = "Онлайн"
if PlayerStatusServerTwo == "offline":
PlayerStatusServerTwo = "Оффлайн"
#print('25')
time.sleep(1)
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Valheim.by - Launcher"))
self.progressBar.setFormat(_translate("MainWindow", "%p%"))
self.commandLinkButton.setText(_translate("MainWindow", "Valheim.by - наш сайт"))
self.commandLinkButton_2.setText(_translate("MainWindow", "Discord сервер"))
self.commandLinkButton.clicked.connect(lambda: webbrowser.open_new_tab('https://valheim.by'))
self.commandLinkButton_2.clicked.connect(lambda: webbrowser.open_new_tab('https://discord.gg/XCA6bPp3qB'))
self.pushButton.setText(_translate("MainWindow", "В ИГРУ"))
self.label.setText(_translate("MainWindow", "Статус серверов: Valheim.by #1 - {0} - {1}\nValheim.by #2 - {2} - {3}".format(PlayerStatusServerOne, PlayerCountServerOne, PlayerStatusServerTwo, PlayerCountServerTwo)))
self.label_3.setText(_translate("MainWindow", "Статус загрузки:"))
self.label_4.setText(_translate("MainWindow", "Проверка завершена"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
t = threading.Thread(target=retranslateUi, args=())
t.start()
sys.exit(app.exec_())
Но программа не запускается, а в консоль print()
выводит, то есть считай цикл работает.
QThread
. Накидаю пример в ответе