python PyQt5/Pyside2 按鈕右擊菜單實(shí)例代碼
更新時(shí)間:2019年08月17日 14:29:11 作者:MakChiKin
本文通過(guò)實(shí)例代碼給大家介紹了python PyQt5/Pyside2 按鈕右擊菜單,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
具體代碼如下所述:
import sys
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *
class MainForm(QMainWindow):
def __init__(self, parent=None):
super(MainForm, self).__init__(parent)
# create button
self.button = QPushButton("test button", self)
self.button.resize(100, 30)
# set button context menu policy
self.button.setContextMenuPolicy(Qt.CustomContextMenu)
self.button.customContextMenuRequested.connect(self.on_context_menu)
# create context menu
self.popMenu = QMenu(self)
self.popMenu.addAction(QAction('test0', self))
self.popMenu.addAction(QAction('test1', self))
self.popMenu.addSeparator()
self.popMenu.addAction(QAction('test2', self))
def on_context_menu(self, point):
# show context menu
self.popMenu.exec_(self.button.mapToGlobal(point))
def main():
app = QApplication(sys.argv)
form = MainForm()
form.show()
app.exec_()
if __name__ == '__main__':
main()
總結(jié)
以上所所述是小編給大家介紹的python PyQt5/Pyside2 按鈕右擊菜單實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
您可能感興趣的文章:
- python編程PyQt5創(chuàng)建按鈕及觸發(fā)點(diǎn)擊事件示例解析
- python GUI庫(kù)圖形界面開發(fā)之PyQt5切換按鈕控件QPushButton詳細(xì)使用方法與實(shí)例
- python GUI庫(kù)圖形界面開發(fā)之PyQt5單選按鈕控件QRadioButton詳細(xì)使用方法與實(shí)例
- python之PyQt按鈕右鍵菜單功能的實(shí)現(xiàn)代碼
- Python中PyQt5/PySide2的按鈕控件使用實(shí)例
- python之pyqt5通過(guò)按鈕改變Label的背景顏色方法
- Python PYQT界面點(diǎn)擊按鈕隨機(jī)變色功能
相關(guān)文章
python讀取文本中數(shù)據(jù)并轉(zhuǎn)化為DataFrame的實(shí)例
下面小編就為大家分享一篇python讀取文本中數(shù)據(jù)并轉(zhuǎn)化為DataFrame的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
python中isoweekday和weekday的區(qū)別及說(shuō)明
這篇文章主要介紹了python中isoweekday和weekday的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

