Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Set Position for Label inside QTab not Working



  • Hi All,
    I am new to pyqt and programming. after referring tutorials and videos, I am using pyqt5 in ubuntu for QT APP Development. My app has 2 tabs and i have to use labels and push buttons. i am able to add button and labels. But for setting the position of label i am using "self.l1.move(10, 50)" after the line"self.l1 = QLabel("NAME")". the label is displayed at its default left and vertical orientation position. the label. move is not working. the same move instruction is working for push button. please help me to find the issue behind and solve the problem. here is the code.
    Code
    import sys
    from PyQt5 import QtCore
    from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout, QLabel, QMessageBox
    from PyQt5.QtCore import QSize

    Creating the main window

    class App(QMainWindow):
    def init(self):
    super().init()
    self.title = 'CAN DRIVER APP'
    self.left = 0
    self.top = 0
    self.width = 1200
    self.height = 600
    self.setWindowTitle(self.title)
    self.setGeometry(self.left, self.top, self.width, self.height)

    	self.tab_widget = MyTabWidget(self)
    	self.setCentralWidget(self.tab_widget)
    
    	self.show()
    

    Creating tab widgets

    class MyTabWidget(QWidget):
    def init(self, parent):
    super(QWidget, self).init(parent)
    self.layout = QVBoxLayout(self)

                # Initialize tab screen
                self.tabs = QTabWidget()
                self.tab1 = QWidget()
                self.tab2 = QWidget()
                self.tabs.resize(1200, 600)
    
                # Add tabs
                self.tabs.addTab(self.tab1, "BATTERY CONTROL")
                self.tabs.addTab(self.tab2, "BATTERY PARAMETER")
    
                # Create first tab
                self.tab1.layout = QVBoxLayout(self)
                l1 = QLabel("NAME")
                l1.move(10, 50)
                l3 = QLabel("DOB")
                l3.move(20, 50)
                l4 = QLabel("gg")
                l4.move(30, 50)
    
                #l1.setText("This is the first tab")
                
                self.tab1.layout.addWidget(l1)
                self.tab1.layout.addWidget(l3)
                self.tab1.layout.addWidget(l4)
                self.tab1.setLayout(self.tab1.layout) 
                     
                self.pybutton = QPushButton('START CHARGING', self.tab1)
                self.pybutton.clicked.connect(self.clickMethod)
                self.pybutton.resize(200, 64)
                self.pybutton.move(100, 100)
                
                self.pybutton1 = QPushButton('START CHARGING1', self.tab1)
                self.pybutton1.clicked.connect(self.clickMethod)
                self.pybutton1.resize(200, 64)
                self.pybutton1.move(400, 400)
                               
                # Create second tab
                self.tab2.layout = QVBoxLayout(self)
                self.l2 = QLabel()
                self.l2.setText("This is the second tab")
                self.l2.move(50,50) 
                self.tab2.layout.addWidget(self.l2)
                self.tab2.setLayout(self.tab2.layout)
    
                # Add tabs to widget
                self.layout.addWidget(self.tabs)
                self.setLayout(self.layout)
    
         def clickMethod(self):
                QMessageBox.about(self, "Charger Control", "Confirm")
    

    if name == 'main':
    app = QApplication(sys.argv)
    ex = App()
    #MyTabWidget.pybutton.show()
    sys.exit(app.exec_())



  • @philipsj33
    You would not normally use any absolute positioning coordinates for widgets. It's not a good idea. Rather you would use Qt's QLayouts, as just about all widgets examples will do. have you started by reading https://doc.qt.io/qt-5/layout.html ?


Log in to reply