LISHUZUOXUN_yangjiang/LSZXPagesLibrary/pop_windows_wifi.py

175 lines
6.3 KiB
Python
Raw Normal View History

2024-09-23 14:54:15 +08:00
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
class PopWindowsWifi:
def __init__(self, parent):
super().__init__()
self.parent = parent
self.wifi_name = None
self.labels = []
self.wifi_list = []
desktop = QDesktopWidget()
self.screenRect = desktop.availableGeometry()
self.dialog = QDialog(
flags=Qt.CustomizeWindowHint | Qt.WindowCloseButtonHint | Qt.WindowStaysOnTopHint
) # 设置无图标和标题
self.dialog.setStyleSheet("background-color:#ffffff;")
self.dialog.setContentsMargins(0, 0, 0, 0)
self.dialog.setFixedSize(int(self.screenRect.width() * 0.35),
int(self.screenRect.height() * 0.75)) # 设置对话框的宽度为屏幕的35%高度为屏幕的70%
self.cancel_btn = self._init_cancel_button()
self.commit_btn = self._init_commit_button()
self.cancel_btn.clicked.connect(self.cancel_button_action)
self.commit_btn.clicked.connect(self.commit_button_action)
self.top_layout = QHBoxLayout()
self.top_layout.setContentsMargins(20, 0, 0, 0)
self.top_label = QLabel("当前可连接WiFi列表")
self.top_label.setStyleSheet("font: 600 26px \"Microsoft YaHei UI\";")
self.top_layout.addWidget(self.top_label)
# WiFi列表布局
self.list_layout = QVBoxLayout()
self.scroll_area = QScrollArea()
self.scroll_area.setStyleSheet("border:1px solid #bbbbbb;")
self.list_layout.addWidget(self.scroll_area)
self.cur_wifi_frame = QLabel("")
self.btn_layout = QHBoxLayout()
self.btn_layout.addWidget(self.cancel_btn, 50)
self.btn_layout.addWidget(self.commit_btn, 50)
#
self.bottom_layout = QVBoxLayout()
self.bottom_layout.addWidget(self.cur_wifi_frame, 35)
self.bottom_layout.addLayout(self.btn_layout, 65)
# 总布局
self.main_layout = QVBoxLayout()
self.main_layout.addLayout(self.top_layout, 4)
self.main_layout.addLayout(self.list_layout, 80)
self.main_layout.addLayout(self.bottom_layout, 16)
self.dialog.setLayout(self.main_layout)
# 添加遮罩层设置parent确保遮罩层在父页面中显示
self.overlay = QWidget(self.parent)
self.overlay.setStyleSheet("background-color: rgba(0, 0, 0, 0.5);")
self.overlay.resize(self.screenRect.width(), self.screenRect.height())
self.overlay.hide()
# 回调函数
self.wifi_select_action = None
def show(self, wifi_list):
self.overlay.show()
# 更新Wi-Fi列表数据
self.labels.clear()
self.wifi_list = wifi_list
self.cur_wifi_frame.setText("")
scroll_widget = QWidget()
scroll_widget.setStyleSheet("border: none;")
item_layout = QVBoxLayout(scroll_widget)
for wifi_info in self.wifi_list:
item_label = QLabel(wifi_info['name'])
item_label.setStyleSheet(
"font: 24px \"Microsoft YaHei UI\";"
"border:1px solid #a7a7a7;"
"border-radius:4px;"
"background-color:#f4f4f5;"
"padding: 12px 250px 12px 20px;"
"margin:6px;"
"color:#303133;"
)
item_layout.addWidget(item_label)
self.labels.append(item_label)
item_label.mousePressEvent = lambda event, lbl=item_label: self.wifi_name_clicked(event, lbl)
self.scroll_area.setWidget(scroll_widget)
self.dialog.show()
# 初始化取消按钮
@staticmethod
def _init_cancel_button():
cancel_btn = QPushButton("取消")
cancel_btn.setStyleSheet(
"border: 1px solid #222222;"
"color: #222222;"
"padding-top: 20px;"
"padding-bottom: 20px;"
"margin-left: 20px;"
"margin-right: 20px;"
"border-radius: 4px;"
"font: 24px \"Microsoft YaHei UI\";"
)
return cancel_btn
# 初始化确认按钮
@staticmethod
def _init_commit_button():
commit_btn = QPushButton("确定")
commit_btn.setStyleSheet(
"border: 1px solid #409eff;"
"color: #ffffff;"
"padding-top: 20px;"
"padding-bottom: 20px;"
"margin-left: 20px;"
"margin-right: 20px;"
"border-radius: 4px;"
"font: 24px \"Microsoft YaHei UI\";"
"background-color: #409eff;"
)
return commit_btn
# 取消按钮响应事件
def cancel_button_action(self):
self.labels = []
self.dialog.hide()
self.overlay.hide()
# 确认按钮响应事件
def commit_button_action(self):
# 调用链接函数
if self.wifi_select_action:
self.wifi_select_action(self.wifi_name)
self.dialog.hide()
self.overlay.hide()
def connect(self, func):
self.wifi_select_action = func
# 获取当前选中的WiFi名字
def wifi_name_clicked(self, event, label):
index = self.labels.index(label)
self.wifi_name = self.wifi_list[index]["name"]
self.cur_wifi_frame.setText('当前选中的WiFi' + self.wifi_name)
self.cur_wifi_frame.setStyleSheet("font: 24px \"Microsoft YaHei UI\";margin-left:10px;")
for i, l in enumerate(self.labels):
if l == label:
# 选中时的样式
self.labels[i].setStyleSheet(
"font: 24px \"Microsoft YaHei UI\";"
"border: 1px solid #66b1ff;"
"border-radius: 4px;"
"background-color: #ecf5ff;"
"padding: 12px 250px 12px 20px;"
"margin: 6px;"
"color: #303133;"
)
else:
# 未选中时的样式
self.labels[i].setStyleSheet(
"font: 24px \"Microsoft YaHei UI\";"
"border: 1px solid #a7a7a7;"
"border-radius: 4px;"
"background-color: #f4f4f5;"
"padding: 12px 250px 12px 20px;"
"margin: 6px;"
"color: #303133;"
)