Python Forum

Full Version: Button creation on Widget
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

I have created a QPushButtons on the top of the widget, but then I wanted to add those buttons on the left side of the widget.

any suggestion would be appreciated.

Regards,
Maiya
Can you post your code?
Use a toolbar.
Then you can specify where the toolbar is (top, left, right, bottom).
You can also move the toolbar with the mouse.


from PyQt5.QtWidgets import (QMainWindow, QApplication, QVBoxLayout, QHBoxLayout, 
                             QWidget, QPushButton, QToolBar)
from PyQt5.QtCore import Qt


class mainWin(QMainWindow):
    def __init__(self, parent = None):
        super(mainWin, self).__init__(parent)
        self.setupUI()
        
    def setupUI(self):
        self.setGeometry(0, 0, 800, 600)
        central_widget = QWidget()
        vbox = QVBoxLayout()
        central_widget.setLayout(vbox)
        
        tbar = QToolBar("Test")
        btn = QPushButton("Open")
        btn.clicked.connect(self.btn_clicked)
        tbar.addWidget(btn)

        btn_save = QPushButton("Save")
        tbar.addWidget(btn_save)
        
        self.addToolBar(Qt.LeftToolBarArea, tbar)
        
        self.setCentralWidget(central_widget)
        
    def btn_clicked(self):
        print("Open button clicked")


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    win = mainWin()
    win.setWindowTitle("Main Window")
    win.show()

    sys.exit(app.exec_())