42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
|
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QAction, QActionGroup
|
||
|
|
||
|
class MenuExample(QMainWindow):
|
||
|
def __init__(self):
|
||
|
super(MenuExample, self).__init__()
|
||
|
|
||
|
self.init_ui()
|
||
|
|
||
|
def init_ui(self):
|
||
|
self.setWindowTitle('Menu Example')
|
||
|
|
||
|
main_menu = self.menuBar()
|
||
|
|
||
|
file_menu = main_menu.addMenu('File')
|
||
|
|
||
|
# 创建一个 QActionGroup
|
||
|
action_group = QActionGroup(self)
|
||
|
action_group.setExclusive(True) # 设置为单选模式
|
||
|
|
||
|
# 创建两个菜单项,添加到 QActionGroup 中
|
||
|
action1 = QAction('Option 1', self)
|
||
|
action1.setCheckable(True)
|
||
|
action1.setChecked(True)
|
||
|
action1.setIconText("• Option 1")
|
||
|
action_group.addAction(action1)
|
||
|
file_menu.addAction(action1)
|
||
|
|
||
|
action2 = QAction('Option 2', self)
|
||
|
action2.setCheckable(True)
|
||
|
action2.setChecked(False)
|
||
|
action2.setIconText("• Option 2")
|
||
|
action_group.addAction(action2)
|
||
|
file_menu.addAction(action2)
|
||
|
|
||
|
self.setGeometry(100, 100, 800, 600)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app = QApplication([])
|
||
|
window = MenuExample()
|
||
|
window.show()
|
||
|
app.exec_()
|