##QAbstracButton
from PyQt5.Qt import *
import sys
class Btn(QAbstractButton):
def paintEvent(self, QPaintEvent):
# print("绘制按钮")
'''创建一个画家'''
painter=QPainter(self)
'''给画家一个画笔'''
'''创建一个画笔'''
pen=QPen(QColor(111,200,50),10)
'''设置这个笔'''
painter.setPen(pen)
'''画家画'''
painter.drawText(20,20,"社会我顺哥,人狠话不多")
painter.drawEllipse(0,0,100,100)
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('QAbstracButton的学习')
self.resize(500, 500)
self.setup_ui()
def setup_ui(self):
btn=Btn(self)
btn.setText("测试")
btn.resize(100,100)
btn.pressed.connect(lambda :print("点击了这个按钮"))
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('QAbstracButton的学习')
self.resize(500, 500)
self.setup_ui()
def setup_ui(self):
btn=QPushButton(self)
btn.setText("测试")
btn.resize(100,100)
btn.setIcon(QIcon("code.png"))
btn.pressed.connect(lambda :print("点击了这个按钮"))
'''菜单的设置'''
menu=QMenu(self)
'''子菜单最近打开'''
open_recent_menu = QMenu(self)
open_recent_menu.setTitle("最近打开")
'''子菜单的设置'''
# new_ation=QAction(self)
# new_ation.setText("新建")
# new_ation.setIcon(QIcon("code.png"))
new_ation = QAction(QIcon("code.png"),"新建",self)
new_ation.triggered.connect(lambda :print("新建文件"))
open_ation = QAction(QIcon("code.png"), "打开", self)
open_ation.triggered.connect(lambda: print("打开文件"))
exit_ation = QAction(QIcon("code.png"), "退出", self)
exit_ation.triggered.connect(lambda: print("退出程序"))
file_action=QAction("Python-GUI编程-PyQt5",self)
'''添加菜单'''
menu.addAction(new_ation)
menu.addAction(open_ation)
menu.addMenu(open_recent_menu)
open_recent_menu.addAction(file_action)
#分割线
menu.addSeparator()
menu.addAction(exit_ation)
btn.setMenu(menu)
btn2=QPushButton(self)
btn2.setText("btn2")
btn2.move(200,200)
btn2.setAutoDefault(True)
print(btn.autoDefault())
print(btn2.autoDefault())
btn2.setDefault(True)
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
from PyQt5.Qt import *
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('QAbstracButton的学习')
self.resize(500, 500)
self.setup_ui()
def contextMenuEvent(self, evt):
print("默认上下文菜单调用这个方法")
'''菜单的设置'''
menu=QMenu(self)
'''子菜单最近打开'''
open_recent_menu = QMenu(self)
open_recent_menu.setTitle("最近打开")
'''子菜单的设置'''
# new_ation=QAction(self)
# new_ation.setText("新建")
# new_ation.setIcon(QIcon("code.png"))
new_ation = QAction(QIcon("code.png"),"新建",self)
new_ation.triggered.connect(lambda :print("新建文件"))
open_ation = QAction(QIcon("code.png"), "打开", self)
open_ation.triggered.connect(lambda: print("打开文件"))
exit_ation = QAction(QIcon("code.png"), "退出", self)
exit_ation.triggered.connect(lambda: print("退出程序"))
file_action=QAction("Python-GUI编程-PyQt5",self)
'''添加菜单'''
menu.addAction(new_ation)
menu.addAction(open_ation)
menu.addMenu(open_recent_menu)
open_recent_menu.addAction(file_action)
#分割线
menu.addSeparator()
menu.addAction(exit_ation)
menu.exec_(evt.pos())
def setup_ui(self):
btn=QPushButton(self)
btn.setText("测试")
btn.resize(100,100)
btn.setIcon(QIcon("code.png"))
btn.pressed.connect(lambda :print("点击了这个按钮"))
'''菜单的设置'''
menu=QMenu(self)
'''子菜单最近打开'''
open_recent_menu = QMenu(self)
open_recent_menu.setTitle("最近打开")
'''子菜单的设置'''
# new_ation=QAction(self)
# new_ation.setText("新建")
# new_ation.setIcon(QIcon("code.png"))
new_ation = QAction(QIcon("code.png"),"新建",self)
new_ation.triggered.connect(lambda :print("新建文件"))
open_ation = QAction(QIcon("code.png"), "打开", self)
open_ation.triggered.connect(lambda: print("打开文件"))
exit_ation = QAction(QIcon("code.png"), "退出", self)
exit_ation.triggered.connect(lambda: print("退出程序"))
file_action=QAction("Python-GUI编程-PyQt5",self)
'''添加菜单'''
menu.addAction(new_ation)
menu.addAction(open_ation)
menu.addMenu(open_recent_menu)
open_recent_menu.addAction(file_action)
#分割线
menu.addSeparator()
menu.addAction(exit_ation)
btn.setMenu(menu)
btn2=QPushButton(self)
btn2.setText("btn2")
btn2.move(200,200)
btn2.setAutoDefault(True)
print(btn.autoDefault())
print(btn2.autoDefault())
btn2.setDefault(True)
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
def show_monu(point):
print("自定义上下文菜单",point)
print("默认上下文菜单调用这个方法")
'''菜单的设置'''
self=window
menu = QMenu(self)
'''子菜单最近打开'''
open_recent_menu = QMenu(self)
open_recent_menu.setTitle("最近打开")
'''子菜单的设置'''
# new_ation=QAction(self)
# new_ation.setText("新建")
# new_ation.setIcon(QIcon("code.png"))
new_ation = QAction(QIcon("code.png"), "新建", self)
new_ation.triggered.connect(lambda: print("新建文件"))
open_ation = QAction(QIcon("code.png"), "打开", self)
open_ation.triggered.connect(lambda: print("打开文件"))
exit_ation = QAction(QIcon("code.png"), "退出", self)
exit_ation.triggered.connect(lambda: print("退出程序"))
file_action = QAction("Python-GUI编程-PyQt5", self)
'''添加菜单'''
menu.addAction(new_ation)
menu.addAction(open_ation)
menu.addMenu(open_recent_menu)
open_recent_menu.addAction(file_action)
# 分割线
menu.addSeparator()
menu.addAction(exit_ation)
dest_point=window.mapToGlobal(point)
menu.exec_(dest_point)
window.setContextMenuPolicy(Qt.CustomContextMenu)
window.customContextMenuRequested.connect(show_monu)
window.show()
sys.exit(app.exec_())