Home > Software engineering >  How to make a floating window when I press the user button in pyqt5?
How to make a floating window when I press the user button in pyqt5?

Time:01-23

I am new to the world of pyqt5 I have created a window using this code:

class Ui_menu(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui_menu, self).__init__() # Call the inherited classes __init__ method
        uic.loadUi('Windows/menu.ui', self) # Load the .ui file
        self.show()

    app = QtWidgets.QApplication(sys.argv)
    window = Ui_menu()
    app.exec_()

I want to know if you can create an animation when you click a button at the top and open a floating window as in the attached image.

enter image description here

CodePudding user response:

without your .ui i can say that your button has become a menu via

setMenu(self.yourButtonName)  #...

CodePudding user response:

look this code

from PyQt5.QtWidgets import *
from PyQt5.QtCore import QSize, QPoint, Qt
from PyQt5.QtGui import QIcon



class MyProxyStyle(QProxyStyle):
    pass
    def pixelMetric(self, QStyle_PixelMetric, option=None, widget=None):

        if QStyle_PixelMetric == QStyle.PM_SmallIconSize:
            return 100
        else:
            return QProxyStyle.pixelMetric(self, QStyle_PixelMetric, option, widget)



class main(QMainWindow):
    def __init__(self):
        super().__init__()

        self.central = QWidget()

        self.toolbar = toolbar()

        self.button1 = toolbutton("your_icon")
        self.button2 = toolbutton("your_icon")
        
        self.toolbar.addWidget(self.button1)          
        self.toolbar.addWidget(self.button2)

        self.button1.clicked.connect(lambda: self.open_menu(self.button1))
        self.button2.clicked.connect(lambda: self.open_menu(self.button2))
        
        self.addToolBar(Qt.ToolBarArea.TopToolBarArea, self.toolbar)

        self.setCentralWidget(self.central)
        self.resize(600,400)
        self.show()

    
    def open_menu(self, obj):
        self.menu = QMenu()
        self.menu.setStyle(MyProxyStyle())

        self.menu.addAction(QIcon("your_icon"), "Person")
        self.menu.addMenu("Configuration")
        self.menu.addSeparator()
        self.menu.addMenu("Profile")

        pos = self.mapToGlobal(QPoint(obj.x(), obj.y() obj.height()))

        self.menu.exec(pos)



class toolbar(QToolBar):
    def __init__(self):
        super().__init__()

        self.setIconSize(QSize(80,80))
        self.setMinimumHeight(80)
        self.setMovable(False)



class toolbutton(QToolButton):
    def __init__(self, icon):
        super().__init__()

        self.setIcon(QIcon(icon))
        self.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonIconOnly)



app = QApplication([])
window = main()
app.exec()
  •  Tags:  
  • Related