python3 Tutorial: Implementing the updated interface python3

Today small for everyone to share an update interface realized in python3 having a good reference value, we want to help. Xiao Bian together to follow up to see it
I ado, directly on the code!

from PyQt5.QtCore import QThread , pyqtSignal, QDateTime , QObject
from PyQt5.QtWidgets import QApplication, QDialog, QLineEdit
import time
import sys
  
class BackendThread(QObject):
 # 通过类成员对象定义信号
 update_date = pyqtSignal(str)
  
 # 处理业务逻辑
 def run(self):
  while True:
   data = QDateTime.currentDateTime()
   currTime = data.toString("yyyy-MM-dd hh:mm:ss")
   self.update_date.emit( str(currTime) )
   time.sleep(1)
  
class Window(QDialog):
 def __init__(self):
  QDialog.__init__(self)
  self.setWindowTitle('PyQt 5界面实时更新例子')
  self.resize(400, 100)
  self.input = QLineEdit(self)
  self.input.resize(400, 100)
  self.initUI()
  
 def initUI(self):
  # 创建线程
  self.backend = BackendThread()
  # 连接信号
  self.backend.update_date.connect(self.handleDisplay)
  self.thread = QThread()
  self.backend.moveToThread(self.thread)
  # 开始线程
  self.thread.started.connect(self.backend.run)
  self.thread.start()
  
 # 将当前时间输出到文本框
 def handleDisplay(self, data):
  self.input.setText(data)
  
if __name__ == '__main__':
 app = QApplication(sys.argv)
 win = Window()
 win.show() 
 sys.exit(app.exec_())

Supplementary Development:

python auto-refresh the page code

1 Introduction

1. Open the Web page

2) to achieve regularly updated

It can be seen opening and closing automatically after several pages, number of browsers from automatically become 118 119 Here Insert Picture Description
2 functions to achieve

1) A method for

from time import sleep
 
from selenium import webdriver
 
driver= webdriver.Chrome() # 需要 下载 对应浏览器 驱动到 python 安装目录
driver.get("https://blog.csdn.net/qq_27061049/article/details/90577597") # 刷新网址
 
for i in range(10000): # 刷新次数
 driver.refresh() # 刷新网页
 sleep(5) # 五秒一次

2) Another method

Contents
Here Insert Picture Description
1) openweb.py

# -*- coding: utf-8 -*-
 
import sys
 
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
 
 
class WebView(QWebEngineView):
 def __init__(self):
  super(WebView, self).__init__()
  url = 'https://blog.csdn.net/qq_27061049/article/details/89711766' # 自定义刷新的网页
  self.load(QUrl(url))
  self.showMinimized() #窗口最小化
  self.show()
  self.thread = Worker() # 创建线程实例
  self.thread.sinOut.connect(self.reloadWeb) # 信号绑定槽函数
  self.thread.start() # 开启线程
 
 def reloadWeb(self):
  self.reload() #刷新网页
 
class Worker(QThread):
 sinOut = pyqtSignal() # 创建新的信号,并且有参数
 num = 0
 def __init__(self, parent=None): # 构造方法 创建号对象之后,会自动调用
  super(Worker, self).__init__(parent)
 
 
 def __del__(self): # 析构函数 再对象被删除 和 回收的时候调用
  self.wait()
 
 def run(self):
  for i in range(1000):
   # 发出信号
   self.sinOut.emit() # 给信号传参字符串,并发送
   # 线程休眠66秒
   self.sleep(66)
   Worker.num = Worker.num + 1
   print (str(Worker.num) + " 次刷新")
 
if __name__ == '__main__':
 app = QApplication(sys.argv)
 web = WebView()
 print('### exec succeed !')
 sys.exit(app.exec_())

We recommend the python learning sites to see how seniors are learning! From basic python script, reptiles, django, data mining, programming techniques, as well as to combat zero-based sorting data items, given to every love learning python small partner! Python veteran day have to explain the timing of technology, to share some of the ways to learn and need to pay attention to small details, click to join our gathering python learner

Published 43 original articles · won praise 22 · views 60000 +

Guess you like

Origin blog.csdn.net/haoxun06/article/details/104544581