1. python pyqt6 QPushButton 设定快捷键
import os import sys from PyQt6.QtWidgets import QPushButton from PyQt6.QtGui import QIcon from PyQt6.QtCore import QSize # ......中间省略 # 初始化按钮 self.btn = QPushButton('', self) self.btn.move(self.width() - 100, 5) # 设置位置 x,y self.btn.setStyleSheet('border:none;background:transparent;')# 设定背景颜色透明,设定无边框,image:url("./close.png"); self.btn.setIcon(QIcon(get_icon('top.png'))) # 设置按钮图标,get_icon是因为打包后找不到文件,所以单独判断打包前后的路径 self.btn.setIconSize(QSize(20, 20)) # 设置图标尺寸 self.btn.clicked.connect(self.xxx) # 设置点击事件 self.btn.setShortcut('f2') # 设定按钮的快捷键 self.btn.setFixedWidth(20) # 设定固定宽度 def xxx(): print("测试") def get_icon(localIConName): # 避免打包后根据相对路径找不到图片 if hasattr(sys,'_MEIPASS'): # print(" exe ") path = os.path.join(sys._MEIPASS, 'icon', localIConName) else: # print(" local ") path = os.path.join('icon',localIConName) return path
标签:设定,记录,python,self,使用,import,path,btn,icon From: https://www.cnblogs.com/uoky/p/17965756