html简单⽹页模板代码_实现⼀个简单的html⽹页预览器本次分享实现⼀个简单的页⾯图⽚加载功能,展⽰所⽤精美图⽚⽤pyecharts绘制,更多美图可关注Python与算法社区获取源代码。本次代码结构由两个⽂件组成,主页⾯设置控制键和功能函数,通过页⾯⿏标点击事件响应调⽤open_html.py⽂件,新建窗体显⽰html⽹页图⽚。
html⽹页窗体加载
#第⼀部分:主函数html_view.py⽂件,⽹页查看主页,点击标签lable响应循环切换加载html⽹页
dive是什么意思翻译from PyQt5.Qt import *
以下哪些web常见框架import os
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView
class EventFilter(QWidget):
def __init__(self,parent=None):
super(EventFilter, self).__init__(parent)
self.file_name = self.setup_dir_fullname()
self.index = 0
self.setWindowTitle("html_view")
# 设置窗⼝图标
self.setWindowIcon(QIcon('icons/two.ico'))
self.label1 = QLabel("prev")
self.label2 = QLabel("next")
self.LabelState=QLabel("测试提⽰")
self.image1 = QImage("G:/Projectfile/icons/back.png")
self.image2 = QImage("G:/Projectfile/icons/next.png")
# 关键1、对要过滤的控件设置installEventFilter,这些控件的所有事件都会被eventFilter函数接受并处理。
self.label1.installEventFilter(self)
self.label2.installEventFilter(self)
#设置⽹格布局
mainLayout=QGridLayout(self)
mainLayout.addWidget(self.label1,500,0)
mainLayout.addWidget(self.label2, 500, 2)
mainLayout.addWidget(self.LabelState, 600, 1)
self.setLayout(mainLayout)
print(self.file_name)
# 关键2、在eventFiltr函数中处理这些控件的事件信息。
def eventFilter(self, watched,event):
if watched == self.label1:  #只对label1的点击事件进⾏过滤,重写⾏为,其他事件忽略
#⿏标按下事件(这⾥仅设置⿏标左键响应)
pe() ==QEvent.MouseButtonPress:
mouseEvent = QMouseEvent(event)
if mouseEvent.buttons() == Qt.LeftButton:
self.open(self.prev())
self.LabelState.setText(self.prev())
#缩放图⽚
transform=QTransform()
transform.scale(0.5,0.5)
tmp=ansformed(transform)
tmp2 = ansformed(transform)
self.label1.setPixmap(QPixmap.fromImage(tmp))
self.label2.setPixmap(QPixmap.fromImage(tmp2))
# ⿏标释放事件
pe() == QEvent.MouseButtonRelease:
self.label1.setPixmap(QPixmap.fromImage(self.image1))
if watched == self.label2:  # 只对label2的点击事件进⾏过滤,重写⾏为,其他事件忽略
pe() == QEvent.MouseButtonPress:
mouseEvent = QMouseEvent(event)
if mouseEvent.buttons() == Qt.LeftButton:
self.())
self.LabelState.())
# 转换图⽚⼤⼩
transform = QTransform()
transform.scale(0.5, 0.5)
tmp2 = ansformed(transform)
self.label2.setPixmap(QPixmap.fromImage(tmp2))
# ⿏标释放事件
pe() == QEvent.MouseButtonRelease:
self.label2.setPixmap(QPixmap.fromImage(self.image2))
return QWidget.eventFilter(self,watched,event)
def open(self,file_name1):  # 显⽰窗体2
import open_html
self.second = Window(file_name1)
self.second.show()
def setup_dir_fullname(self):
# 初始化设置存储HTML⽂件的⽂件夹名称,默认“pic_html”,这⾥存放需要预览的html⽂件。        pyechart_dir = 'pic_html'
if not os.path.isdir(pyechart_dir):
os.mkdir(pyechart_dir)
openstack安装部署教程self.path_dir_pyechart_html = os.getcwd() + os.sep + pyechart_dir
list
###等效代码###
# list = []
# for dirpath, dirnames, filenames in os.walk(self.path_dir_pyechart_html):
#    for filepath in filenames:
#        list.append(os.path.join(filepath))
def prev(self):
return self.show_file(-1)
def next(self):
def show_file(self, n):
self.index += n
if self.index 0:
self.index = (len(self.file_name) - 1)
if self.index > (len(self.file_name) - 1):
self.index = 0
self.file_name1 = self.file_name[self.index]
return self.file_name1
if __name__ == '__main__':
import sys
# 1、创建⼀个应⽤程序对象
app = QApplication(sys.argv)
# 2、控件的操作html个人网页完整代码怎么看
# 创建控件
window = EventFilter()
# 展⽰控件
window.show()
# 3、应⽤程序的执⾏,进⼊到消息循环
<_())
#第⼆部分:调⽤open_html.py⽂件,新建窗体显⽰html
import os
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView
import sys
class myWindow(QWidget):
def __init__(self,file_name):
super().__init__()
self.file_name=file_name
self.setWindowTitle("play")
# 设置窗⼝图标
self.setWindowIcon(QIcon('icons/pic.ico'))
self.mainLayout(self.file_name)
def mainLayout(self,file_name):
pyechart_dir = 'pic_html'
if not os.path.isdir(pyechart_dir):
os.mkdir(pyechart_dir)
self.path_dir_pyechart_html = os.getcwd() + os.sep + pyechart_dir        path_pyechart = self.path_dir_pyechart_html + os.sep +file_name        self.mainhboxLayout = QHBoxLayout(self)
self.mainhboxLayout.addWidget(self.frame)
self.hboxLayout = QHBoxLayout(self.frame)
# ⽹页嵌⼊PyQt5
# 打开本地html⽂件#使⽤绝对地址定位,在地址前⾯加上 file:/// ,将地址的 \ 改为/        Html.load(QUrl.fromLocalFile("file:/{0}".format(path_pyechart)))
component of vector
self.hboxLayout.Html)
self.setLayout(self.mainhboxLayout)
#测试
# if __name__ == '__main__':
#    app = QApplication(sys.argv)
#
#    main = myWindow(file_name)
#
#    main.show()
#    # main.showMaximized()
#    _())
图⽚查看器(代码参考⽹络)
import os
import tkinter
ssagebox
from PIL import Image, ImageTk
# 创建tkinter应⽤程序窗⼝
root = tkinter.Tk()
# 设置窗⼝的⼤⼩和位置
vue
# 不允许改变窗⼝的⼤⼩
# 设置窗⼝主题
root.iconbitmap(r'.\Projectfile\test\two.ico')
# 获取当前⽂件夹中所有图⽚⽂件列表
suffix = ('.jpg', '.bmp', '.png')
pics = [p for p in os.listdir('./Projectfile/test') dswith(suffix)] pics.sort(key=lambda item: int(item[:item.index('.')]))
current = 0
def changePic(flag):
global current
new = current + flag
if new 0:
# 获取要切换图⽚⽂件名
pic = pics[new]
# 创建Image对象并进⾏缩放
im = Image.open('./Projectfile/test/{}'.format(pic))
w, h = im.size
# 这⾥假设⽤来显⽰图⽚的Label组件尺⼨为 400x600
if w > 400:
h = int(h * 400 / w)
w = 400
if h > 600:
w = int(w * 600 / h)
h = 600
im = im.resize((w, h))
# 创建image对象,并设置Label组件图⽚
im1 = ImageTk.PhotoImage(im)
lbPic['image'] = im1
lbPic.image = im1
current = new
# 上⼀张的按钮
def btnPreClick():
changePic(-1)
# 下⼀张按钮
def btnNextClick():
changePic(1)
def get_ico(path):
ico_img = Image.open(path).resize((32, 32))
icoBtn = ImageTk.PhotoImage(image=ico_img)
return icoBtn
pr = get_ico(r'.\Projectfile\icons\back.png')
nt = get_ico(r'.\Projectfile\icons\next.png')
btnPre = tkinter.Button(root, image=pr, command=btnPreClick) btnPre.place(x=100, y=20, width=80, height=30)
btnNext = tkinter.Button(root, image=nt, command=btnNextClick) btnNext.place(x=230, y=20, width=80, height=30)
# ⽤来显⽰图⽚的Label组件
lbPic = tkinter.Label(root, text='test', width=400, height=600)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。