安装
安装
pip install PyQt5==5.7.1
pip install PyQtWebEngine
1、创建窗口,按百分比划分
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtWidgets import QApplication, QWidget
# 创建应用程序实例
app = QApplication([])
# 创建主窗口
window = QWidget()
window.setWindowTitle("大得ERP")
# 定义窗口大小占屏幕尺寸的百分比
width_percent = 0.7 # 宽度占屏幕宽度的百分比,可根据需求修改
height_percent = 0.7 # 高度占屏幕高度的百分比,可根据需求修改
# 获取屏幕可用几何信息(去除任务栏等占用区域后的屏幕尺寸)
screen = QGuiApplication.primaryScreen()
screen_rect = screen.availableGeometry()
# 根据百分比计算窗口的实际尺寸
window_width = int(screen_rect.width() * width_percent)
window_height = int(screen_rect.height() * height_percent)
# 设置窗口大小
window.resize(window_width, window_height)
# 显示窗口
window.show()
if __name__ == '__main__':# 进入消息循环app.exec_()
2、加个点击按钮
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton# 创建应用程序实例
app = QApplication([])
# 创建主窗口
window = QWidget()
window.setWindowTitle("万邦ERP")
# 定义窗口大小占屏幕尺寸的百分比
width_percent = 0.7 # 宽度占屏幕宽度的百分比,可根据需求修改
height_percent = 0.7 # 高度占屏幕高度的百分比,可根据需求修改
# 获取屏幕可用几何信息(去除任务栏等占用区域后的屏幕尺寸)
screen = QGuiApplication.primaryScreen()
screen_rect = screen.availableGeometry()
# 根据百分比计算窗口的实际尺寸
window_width = int(screen_rect.width() * width_percent)
window_height = int(screen_rect.height() * height_percent)
# 设置窗口大小
window.resize(window_width, window_height)
# 显示窗口
window.show()def button_click():print("按钮被点击了")
# 创建按钮并添加到窗口
button = QPushButton("点击我", window)
# 将按钮的点击信号添加到button_click上,点击就触发了button_click方法
button.clicked.connect(button_click)
button.show()if __name__ == '__main__':# 进入消息循环app.exec_()
3、创建新窗口,保活
# 把new_window窗口变量升级全局,不然加载完button_click,就销毁了
new_window = None
def button_click():global new_windownew_window = QWidget()new_window.setWindowTitle("新窗口")new_window.resize(400, 300)new_window.show()print("按钮被点击了")# 创建按钮并添加到窗口
button = QPushButton("点击我创建新窗口", window)
# 将按钮的点击信号添加到button_click上,点击就触发了button_click方法
button.clicked.connect(button_click)
button.show()
4、加载网页
from PyQt5.QtCore import QUrl
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QHBoxLayout# 创建应用程序实例
app = QApplication([])
# 创建主窗口
window = QWidget()
window.setWindowTitle("大得ERP")
# 定义窗口大小占屏幕尺寸的百分比
width_percent = 0.7 # 宽度占屏幕宽度的百分比,可根据需求修改
height_percent = 0.7 # 高度占屏幕高度的百分比,可根据需求修改
# 获取屏幕可用几何信息(去除任务栏等占用区域后的屏幕尺寸)
screen = QGuiApplication.primaryScreen()
screen_rect = screen.availableGeometry()
# 根据百分比计算窗口的实际尺寸
window_width = int(screen_rect.width() * width_percent)
window_height = int(screen_rect.height() * height_percent)
# 设置窗口大小
window.resize(window_width, window_height)
# 显示窗口
window.show()# 把new_window窗口变量升级全局,不然加载完button_click,就销毁了
new_window = None
def button_click():global new_windownew_window = QWidget()new_window.setWindowTitle("大得ERP应用端")# 创建垂直布局管理器layout = QVBoxLayout()# 创建QWebEngineView实例用于加载网页web_view = QWebEngineView(new_window)# 加载指定的网页,这里以百度为例,你可以替换成任何你想加载的网址web_view.load(QUrl("http://www.baidu.com"))# 将QWebEngineView添加到布局中layout.addWidget(web_view)# 将布局应用到窗口new_window.setLayout(layout)screen = QGuiApplication.primaryScreen()screen_rect = screen.availableGeometry()# 根据百分比计算窗口的实际尺寸window_width = int(screen_rect.width())window_height = int(screen_rect.height())new_window.resize(window_width, window_height)new_window.show()print("按钮被点击了")
def go_backward(self):self.page.triggerAction(QWebEnginePage.Back)def go_forward(self):self.page.triggerAction(QWebEnginePage.Forward)def refresh_page(self):self.page.triggerAction(QWebEnginePage.Reload)# 创建按钮并添加到窗口
button = QPushButton("点击我创建新窗口", window)
# 将按钮的点击信号添加到button_click上,点击就触发了button_click方法
button.clicked.connect(button_click)
button.show()if __name__ == '__main__':# 进入消息循环app.exec_()