PyQT5 : Qlabel overlay eachother even after using grid [duplicate]










-1
















This question already has an answer here:



  • Wrong layout in PyQT5?

    1 answer



I'm new and I just want to learn how to code some UI. My problem with PyQt5 is the following.



I just create 3 labels (each named label, label1 and label2). One is supposed to contain just text, another one is supposed to print the time and the third one is supposed to print the position (x, y) of the mouse.



When I use the grid layout, it just print Qlabels at the same place even if I specify a different position with grid add widget.



Any help is appreciated. Thank you in advance.



My code :



#Source : https://stackoverflow.com/questions/46007891/setting-background-color-of-pyqt5-qwidget
import sys
from PyQt5.QtWidgets import (QMainWindow, QWidget, QLabel, QApplication, QToolTip, QPushButton,
QMessageBox, QGridLayout, QHBoxLayout, QVBoxLayout, QTextEdit)
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtCore import Qt, QTime

class BgColorExperiment(QMainWindow):
def __init__(self):
super().__init__()
# moves the widget to a position on the screen at x=300, y=300 coordinates
self.left = 300
self.top = 300
# resizes the widget (250px wide and 150px high)
self.width = 450
self.height = 450
# title of Widget shown in the titlebar
self.title ='Suivi des BDs crées'
# sets the application icon
self.setWindowIcon(QIcon('suivibdicon.png'))
# Set the status bar
self.statusBar().showMessage('Engineered by idiots')
# parent
self.init_ui()

def init_ui(self):
# Artiste
self.setObjectName('MainWidget')
self.setStyleSheet("""
#MainWidget
background-color: #333;

.QLabel
color: green;
background-color: #333;
selection-color: yellow;
selection-background-color: blue;
font-family : Consolas;
font-size: 10pt;

.QToolTip
background-color: #333;
color: #fff;
border: red solid 1px;
opacity: 100;
font-family : Consolas;
font-size: 10pt;

.QMessageBox
background-color: #333;

QStatusBar
color: green;
font-family : Consolas;
font-size: 10pt;

""")
# violence
self.label = QLabel('position of the mouse', self)
self.label1 = QLabel('some words 一些单词', self)
self.label2 = QLabel('digital clock', self)

grid = QGridLayout()

# just blablabla
grid.addWidget(self.label1, 0, 0)

# Digital Clock
grid.addWidget(self.label2, 1, 0)

# Mouse tracking
x, y = 0, 0
self.text = "x: 0, y: 1".format(x, y)
self.label = QLabel(self.text, self) # display the x and y coordinates of a mouse pointer in a label widget.
grid.addWidget(self.label, 2, 0)
self.setMouseTracking(True)

# # push button widget and set a tooltip (useless)
# #btn = QPushButton('Button', self)
# #btn.setToolTip('This is a <b>QPushButton</b> widget for refresh')
# #btn.resize(btn.sizeHint())
# #hbox.addWidget(btn)

# set the grid
self.setLayout(grid)
# set the window size using the setGeometry
self.setGeometry(self.left, self.top, self.width, self.height)
# set the window title
self.setWindowTitle(self.title)
self.show() # displays the widget on the screen

def closeEvent(self, event): # Define behavior when clicking on the x button on the title bar
reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()

def mouseMoveEvent(self, e): # for following the position of the mouse
x, y = e.x(), e.y()
text = "x: 0, y: 1".format(x, y)
self.label.setText(text)

def showTime(self): # for printing the time (format = 'hh:mm:ss')
time = QTime.currentTime()
text = time.toString('hh:mm:ss')
self.label2.setText(text)

if __name__ == '__main__':
APP = QApplication(sys.argv)
EXP = BgColorExperiment()
sys.exit(APP.exec_()) # mainloop of the application









share|improve this question















marked as duplicate by eyllanesc, Community Nov 15 '18 at 0:57


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















    -1
















    This question already has an answer here:



    • Wrong layout in PyQT5?

      1 answer



    I'm new and I just want to learn how to code some UI. My problem with PyQt5 is the following.



    I just create 3 labels (each named label, label1 and label2). One is supposed to contain just text, another one is supposed to print the time and the third one is supposed to print the position (x, y) of the mouse.



    When I use the grid layout, it just print Qlabels at the same place even if I specify a different position with grid add widget.



    Any help is appreciated. Thank you in advance.



    My code :



    #Source : https://stackoverflow.com/questions/46007891/setting-background-color-of-pyqt5-qwidget
    import sys
    from PyQt5.QtWidgets import (QMainWindow, QWidget, QLabel, QApplication, QToolTip, QPushButton,
    QMessageBox, QGridLayout, QHBoxLayout, QVBoxLayout, QTextEdit)
    from PyQt5.QtGui import QIcon, QFont
    from PyQt5.QtCore import Qt, QTime

    class BgColorExperiment(QMainWindow):
    def __init__(self):
    super().__init__()
    # moves the widget to a position on the screen at x=300, y=300 coordinates
    self.left = 300
    self.top = 300
    # resizes the widget (250px wide and 150px high)
    self.width = 450
    self.height = 450
    # title of Widget shown in the titlebar
    self.title ='Suivi des BDs crées'
    # sets the application icon
    self.setWindowIcon(QIcon('suivibdicon.png'))
    # Set the status bar
    self.statusBar().showMessage('Engineered by idiots')
    # parent
    self.init_ui()

    def init_ui(self):
    # Artiste
    self.setObjectName('MainWidget')
    self.setStyleSheet("""
    #MainWidget
    background-color: #333;

    .QLabel
    color: green;
    background-color: #333;
    selection-color: yellow;
    selection-background-color: blue;
    font-family : Consolas;
    font-size: 10pt;

    .QToolTip
    background-color: #333;
    color: #fff;
    border: red solid 1px;
    opacity: 100;
    font-family : Consolas;
    font-size: 10pt;

    .QMessageBox
    background-color: #333;

    QStatusBar
    color: green;
    font-family : Consolas;
    font-size: 10pt;

    """)
    # violence
    self.label = QLabel('position of the mouse', self)
    self.label1 = QLabel('some words 一些单词', self)
    self.label2 = QLabel('digital clock', self)

    grid = QGridLayout()

    # just blablabla
    grid.addWidget(self.label1, 0, 0)

    # Digital Clock
    grid.addWidget(self.label2, 1, 0)

    # Mouse tracking
    x, y = 0, 0
    self.text = "x: 0, y: 1".format(x, y)
    self.label = QLabel(self.text, self) # display the x and y coordinates of a mouse pointer in a label widget.
    grid.addWidget(self.label, 2, 0)
    self.setMouseTracking(True)

    # # push button widget and set a tooltip (useless)
    # #btn = QPushButton('Button', self)
    # #btn.setToolTip('This is a <b>QPushButton</b> widget for refresh')
    # #btn.resize(btn.sizeHint())
    # #hbox.addWidget(btn)

    # set the grid
    self.setLayout(grid)
    # set the window size using the setGeometry
    self.setGeometry(self.left, self.top, self.width, self.height)
    # set the window title
    self.setWindowTitle(self.title)
    self.show() # displays the widget on the screen

    def closeEvent(self, event): # Define behavior when clicking on the x button on the title bar
    reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
    if reply == QMessageBox.Yes:
    event.accept()
    else:
    event.ignore()

    def mouseMoveEvent(self, e): # for following the position of the mouse
    x, y = e.x(), e.y()
    text = "x: 0, y: 1".format(x, y)
    self.label.setText(text)

    def showTime(self): # for printing the time (format = 'hh:mm:ss')
    time = QTime.currentTime()
    text = time.toString('hh:mm:ss')
    self.label2.setText(text)

    if __name__ == '__main__':
    APP = QApplication(sys.argv)
    EXP = BgColorExperiment()
    sys.exit(APP.exec_()) # mainloop of the application









    share|improve this question















    marked as duplicate by eyllanesc, Community Nov 15 '18 at 0:57


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















      -1












      -1








      -1


      0







      This question already has an answer here:



      • Wrong layout in PyQT5?

        1 answer



      I'm new and I just want to learn how to code some UI. My problem with PyQt5 is the following.



      I just create 3 labels (each named label, label1 and label2). One is supposed to contain just text, another one is supposed to print the time and the third one is supposed to print the position (x, y) of the mouse.



      When I use the grid layout, it just print Qlabels at the same place even if I specify a different position with grid add widget.



      Any help is appreciated. Thank you in advance.



      My code :



      #Source : https://stackoverflow.com/questions/46007891/setting-background-color-of-pyqt5-qwidget
      import sys
      from PyQt5.QtWidgets import (QMainWindow, QWidget, QLabel, QApplication, QToolTip, QPushButton,
      QMessageBox, QGridLayout, QHBoxLayout, QVBoxLayout, QTextEdit)
      from PyQt5.QtGui import QIcon, QFont
      from PyQt5.QtCore import Qt, QTime

      class BgColorExperiment(QMainWindow):
      def __init__(self):
      super().__init__()
      # moves the widget to a position on the screen at x=300, y=300 coordinates
      self.left = 300
      self.top = 300
      # resizes the widget (250px wide and 150px high)
      self.width = 450
      self.height = 450
      # title of Widget shown in the titlebar
      self.title ='Suivi des BDs crées'
      # sets the application icon
      self.setWindowIcon(QIcon('suivibdicon.png'))
      # Set the status bar
      self.statusBar().showMessage('Engineered by idiots')
      # parent
      self.init_ui()

      def init_ui(self):
      # Artiste
      self.setObjectName('MainWidget')
      self.setStyleSheet("""
      #MainWidget
      background-color: #333;

      .QLabel
      color: green;
      background-color: #333;
      selection-color: yellow;
      selection-background-color: blue;
      font-family : Consolas;
      font-size: 10pt;

      .QToolTip
      background-color: #333;
      color: #fff;
      border: red solid 1px;
      opacity: 100;
      font-family : Consolas;
      font-size: 10pt;

      .QMessageBox
      background-color: #333;

      QStatusBar
      color: green;
      font-family : Consolas;
      font-size: 10pt;

      """)
      # violence
      self.label = QLabel('position of the mouse', self)
      self.label1 = QLabel('some words 一些单词', self)
      self.label2 = QLabel('digital clock', self)

      grid = QGridLayout()

      # just blablabla
      grid.addWidget(self.label1, 0, 0)

      # Digital Clock
      grid.addWidget(self.label2, 1, 0)

      # Mouse tracking
      x, y = 0, 0
      self.text = "x: 0, y: 1".format(x, y)
      self.label = QLabel(self.text, self) # display the x and y coordinates of a mouse pointer in a label widget.
      grid.addWidget(self.label, 2, 0)
      self.setMouseTracking(True)

      # # push button widget and set a tooltip (useless)
      # #btn = QPushButton('Button', self)
      # #btn.setToolTip('This is a <b>QPushButton</b> widget for refresh')
      # #btn.resize(btn.sizeHint())
      # #hbox.addWidget(btn)

      # set the grid
      self.setLayout(grid)
      # set the window size using the setGeometry
      self.setGeometry(self.left, self.top, self.width, self.height)
      # set the window title
      self.setWindowTitle(self.title)
      self.show() # displays the widget on the screen

      def closeEvent(self, event): # Define behavior when clicking on the x button on the title bar
      reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
      if reply == QMessageBox.Yes:
      event.accept()
      else:
      event.ignore()

      def mouseMoveEvent(self, e): # for following the position of the mouse
      x, y = e.x(), e.y()
      text = "x: 0, y: 1".format(x, y)
      self.label.setText(text)

      def showTime(self): # for printing the time (format = 'hh:mm:ss')
      time = QTime.currentTime()
      text = time.toString('hh:mm:ss')
      self.label2.setText(text)

      if __name__ == '__main__':
      APP = QApplication(sys.argv)
      EXP = BgColorExperiment()
      sys.exit(APP.exec_()) # mainloop of the application









      share|improve this question

















      This question already has an answer here:



      • Wrong layout in PyQT5?

        1 answer



      I'm new and I just want to learn how to code some UI. My problem with PyQt5 is the following.



      I just create 3 labels (each named label, label1 and label2). One is supposed to contain just text, another one is supposed to print the time and the third one is supposed to print the position (x, y) of the mouse.



      When I use the grid layout, it just print Qlabels at the same place even if I specify a different position with grid add widget.



      Any help is appreciated. Thank you in advance.



      My code :



      #Source : https://stackoverflow.com/questions/46007891/setting-background-color-of-pyqt5-qwidget
      import sys
      from PyQt5.QtWidgets import (QMainWindow, QWidget, QLabel, QApplication, QToolTip, QPushButton,
      QMessageBox, QGridLayout, QHBoxLayout, QVBoxLayout, QTextEdit)
      from PyQt5.QtGui import QIcon, QFont
      from PyQt5.QtCore import Qt, QTime

      class BgColorExperiment(QMainWindow):
      def __init__(self):
      super().__init__()
      # moves the widget to a position on the screen at x=300, y=300 coordinates
      self.left = 300
      self.top = 300
      # resizes the widget (250px wide and 150px high)
      self.width = 450
      self.height = 450
      # title of Widget shown in the titlebar
      self.title ='Suivi des BDs crées'
      # sets the application icon
      self.setWindowIcon(QIcon('suivibdicon.png'))
      # Set the status bar
      self.statusBar().showMessage('Engineered by idiots')
      # parent
      self.init_ui()

      def init_ui(self):
      # Artiste
      self.setObjectName('MainWidget')
      self.setStyleSheet("""
      #MainWidget
      background-color: #333;

      .QLabel
      color: green;
      background-color: #333;
      selection-color: yellow;
      selection-background-color: blue;
      font-family : Consolas;
      font-size: 10pt;

      .QToolTip
      background-color: #333;
      color: #fff;
      border: red solid 1px;
      opacity: 100;
      font-family : Consolas;
      font-size: 10pt;

      .QMessageBox
      background-color: #333;

      QStatusBar
      color: green;
      font-family : Consolas;
      font-size: 10pt;

      """)
      # violence
      self.label = QLabel('position of the mouse', self)
      self.label1 = QLabel('some words 一些单词', self)
      self.label2 = QLabel('digital clock', self)

      grid = QGridLayout()

      # just blablabla
      grid.addWidget(self.label1, 0, 0)

      # Digital Clock
      grid.addWidget(self.label2, 1, 0)

      # Mouse tracking
      x, y = 0, 0
      self.text = "x: 0, y: 1".format(x, y)
      self.label = QLabel(self.text, self) # display the x and y coordinates of a mouse pointer in a label widget.
      grid.addWidget(self.label, 2, 0)
      self.setMouseTracking(True)

      # # push button widget and set a tooltip (useless)
      # #btn = QPushButton('Button', self)
      # #btn.setToolTip('This is a <b>QPushButton</b> widget for refresh')
      # #btn.resize(btn.sizeHint())
      # #hbox.addWidget(btn)

      # set the grid
      self.setLayout(grid)
      # set the window size using the setGeometry
      self.setGeometry(self.left, self.top, self.width, self.height)
      # set the window title
      self.setWindowTitle(self.title)
      self.show() # displays the widget on the screen

      def closeEvent(self, event): # Define behavior when clicking on the x button on the title bar
      reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
      if reply == QMessageBox.Yes:
      event.accept()
      else:
      event.ignore()

      def mouseMoveEvent(self, e): # for following the position of the mouse
      x, y = e.x(), e.y()
      text = "x: 0, y: 1".format(x, y)
      self.label.setText(text)

      def showTime(self): # for printing the time (format = 'hh:mm:ss')
      time = QTime.currentTime()
      text = time.toString('hh:mm:ss')
      self.label2.setText(text)

      if __name__ == '__main__':
      APP = QApplication(sys.argv)
      EXP = BgColorExperiment()
      sys.exit(APP.exec_()) # mainloop of the application




      This question already has an answer here:



      • Wrong layout in PyQT5?

        1 answer







      python python-3.x pyqt pyqt5 qlabel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 0:13









      eyllanesc

      80.4k103258




      80.4k103258










      asked Nov 14 '18 at 23:16









      SemsemTSemsemT

      35




      35




      marked as duplicate by eyllanesc, Community Nov 15 '18 at 0:57


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by eyllanesc, Community Nov 15 '18 at 0:57


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          void QMainWindow::setCentralWidget(QWidget *widget)



          Sets the given widget to be the main window's central widget.



          Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.



          import sys
          from PyQt5.QtWidgets import (QMainWindow, QWidget, QLabel, QApplication, QToolTip, QPushButton,
          QMessageBox, QGridLayout, QHBoxLayout, QVBoxLayout, QTextEdit)
          from PyQt5.QtGui import QIcon, QFont
          from PyQt5.QtCore import Qt, QTime

          class BgColorExperiment(QMainWindow):
          def __init__(self):
          super().__init__()
          # moves the widget to a position on the screen at x=300, y=300 coordinates
          self.left = 300
          self.top = 300
          # resizes the widget (250px wide and 150px high)
          self.width = 450
          self.height = 450
          # title of Widget shown in the titlebar
          self.title ='Suivi des BDs crées'
          # sets the application icon
          self.setWindowIcon(QIcon('suivibdicon.png'))
          # Set the status bar
          self.statusBar().showMessage('Engineered by idiots')
          # parent
          self.init_ui()

          def init_ui(self):
          # Artiste
          self.setObjectName('MainWidget')
          self.setStyleSheet("""
          #MainWidget
          background-color: #333;

          .QLabel
          color: green;
          background-color: #333;
          selection-color: yellow;
          selection-background-color: blue;
          font-family : Consolas;
          font-size: 10pt;

          .QToolTip
          background-color: #333;
          color: #fff;
          border: red solid 1px;
          opacity: 100;
          font-family : Consolas;
          font-size: 10pt;

          .QMessageBox
          background-color: #333;

          QStatusBar
          color: green;
          font-family : Consolas;
          font-size: 10pt;

          """)
          # violence
          self.label = QLabel('position of the mouse', self)
          self.label1 = QLabel('some words 一些单词', self)
          self.label2 = QLabel('digital clock', self)

          centralWidget = QWidget() # +++++
          self.setCentralWidget(centralWidget) # +++++

          grid = QGridLayout(centralWidget) # +++++ centralWidget

          # just blablabla
          grid.addWidget(self.label1, 0, 0)

          # Digital Clock
          grid.addWidget(self.label2, 1, 0)

          # Mouse tracking
          x, y = 0, 0
          self.text = "x: 0, y: 1".format(x, y)
          self.label = QLabel(self.text, self) # display the x and y coordinates of a mouse pointer in a label widget.
          grid.addWidget(self.label, 2, 0)
          self.setMouseTracking(True)

          # # push button widget and set a tooltip (useless)
          # #btn = QPushButton('Button', self)
          # #btn.setToolTip('This is a <b>QPushButton</b> widget for refresh')
          # #btn.resize(btn.sizeHint())
          # #hbox.addWidget(btn)

          # set the grid
          # self.setLayout(grid) # -----
          # set the window size using the setGeometry
          self.setGeometry(self.left, self.top, self.width, self.height)
          # set the window title
          self.setWindowTitle(self.title)
          self.show() # displays the widget on the screen

          def closeEvent(self, event): # Define behavior when clicking on the x button on the title bar
          reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
          if reply == QMessageBox.Yes:
          event.accept()
          else:
          event.ignore()

          def mouseMoveEvent(self, e): # for following the position of the mouse
          x, y = e.x(), e.y()
          text = "x: 0, y: 1".format(x, y)
          self.label.setText(text)

          def showTime(self): # for printing the time (format = 'hh:mm:ss')
          time = QTime.currentTime()
          text = time.toString('hh:mm:ss')
          self.label2.setText(text)

          if __name__ == '__main__':
          APP = QApplication(sys.argv)
          EXP = BgColorExperiment()
          sys.exit(APP.exec_()) # mainloop of the application


          enter image description here






          share|improve this answer





























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            void QMainWindow::setCentralWidget(QWidget *widget)



            Sets the given widget to be the main window's central widget.



            Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.



            import sys
            from PyQt5.QtWidgets import (QMainWindow, QWidget, QLabel, QApplication, QToolTip, QPushButton,
            QMessageBox, QGridLayout, QHBoxLayout, QVBoxLayout, QTextEdit)
            from PyQt5.QtGui import QIcon, QFont
            from PyQt5.QtCore import Qt, QTime

            class BgColorExperiment(QMainWindow):
            def __init__(self):
            super().__init__()
            # moves the widget to a position on the screen at x=300, y=300 coordinates
            self.left = 300
            self.top = 300
            # resizes the widget (250px wide and 150px high)
            self.width = 450
            self.height = 450
            # title of Widget shown in the titlebar
            self.title ='Suivi des BDs crées'
            # sets the application icon
            self.setWindowIcon(QIcon('suivibdicon.png'))
            # Set the status bar
            self.statusBar().showMessage('Engineered by idiots')
            # parent
            self.init_ui()

            def init_ui(self):
            # Artiste
            self.setObjectName('MainWidget')
            self.setStyleSheet("""
            #MainWidget
            background-color: #333;

            .QLabel
            color: green;
            background-color: #333;
            selection-color: yellow;
            selection-background-color: blue;
            font-family : Consolas;
            font-size: 10pt;

            .QToolTip
            background-color: #333;
            color: #fff;
            border: red solid 1px;
            opacity: 100;
            font-family : Consolas;
            font-size: 10pt;

            .QMessageBox
            background-color: #333;

            QStatusBar
            color: green;
            font-family : Consolas;
            font-size: 10pt;

            """)
            # violence
            self.label = QLabel('position of the mouse', self)
            self.label1 = QLabel('some words 一些单词', self)
            self.label2 = QLabel('digital clock', self)

            centralWidget = QWidget() # +++++
            self.setCentralWidget(centralWidget) # +++++

            grid = QGridLayout(centralWidget) # +++++ centralWidget

            # just blablabla
            grid.addWidget(self.label1, 0, 0)

            # Digital Clock
            grid.addWidget(self.label2, 1, 0)

            # Mouse tracking
            x, y = 0, 0
            self.text = "x: 0, y: 1".format(x, y)
            self.label = QLabel(self.text, self) # display the x and y coordinates of a mouse pointer in a label widget.
            grid.addWidget(self.label, 2, 0)
            self.setMouseTracking(True)

            # # push button widget and set a tooltip (useless)
            # #btn = QPushButton('Button', self)
            # #btn.setToolTip('This is a <b>QPushButton</b> widget for refresh')
            # #btn.resize(btn.sizeHint())
            # #hbox.addWidget(btn)

            # set the grid
            # self.setLayout(grid) # -----
            # set the window size using the setGeometry
            self.setGeometry(self.left, self.top, self.width, self.height)
            # set the window title
            self.setWindowTitle(self.title)
            self.show() # displays the widget on the screen

            def closeEvent(self, event): # Define behavior when clicking on the x button on the title bar
            reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if reply == QMessageBox.Yes:
            event.accept()
            else:
            event.ignore()

            def mouseMoveEvent(self, e): # for following the position of the mouse
            x, y = e.x(), e.y()
            text = "x: 0, y: 1".format(x, y)
            self.label.setText(text)

            def showTime(self): # for printing the time (format = 'hh:mm:ss')
            time = QTime.currentTime()
            text = time.toString('hh:mm:ss')
            self.label2.setText(text)

            if __name__ == '__main__':
            APP = QApplication(sys.argv)
            EXP = BgColorExperiment()
            sys.exit(APP.exec_()) # mainloop of the application


            enter image description here






            share|improve this answer



























              0














              void QMainWindow::setCentralWidget(QWidget *widget)



              Sets the given widget to be the main window's central widget.



              Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.



              import sys
              from PyQt5.QtWidgets import (QMainWindow, QWidget, QLabel, QApplication, QToolTip, QPushButton,
              QMessageBox, QGridLayout, QHBoxLayout, QVBoxLayout, QTextEdit)
              from PyQt5.QtGui import QIcon, QFont
              from PyQt5.QtCore import Qt, QTime

              class BgColorExperiment(QMainWindow):
              def __init__(self):
              super().__init__()
              # moves the widget to a position on the screen at x=300, y=300 coordinates
              self.left = 300
              self.top = 300
              # resizes the widget (250px wide and 150px high)
              self.width = 450
              self.height = 450
              # title of Widget shown in the titlebar
              self.title ='Suivi des BDs crées'
              # sets the application icon
              self.setWindowIcon(QIcon('suivibdicon.png'))
              # Set the status bar
              self.statusBar().showMessage('Engineered by idiots')
              # parent
              self.init_ui()

              def init_ui(self):
              # Artiste
              self.setObjectName('MainWidget')
              self.setStyleSheet("""
              #MainWidget
              background-color: #333;

              .QLabel
              color: green;
              background-color: #333;
              selection-color: yellow;
              selection-background-color: blue;
              font-family : Consolas;
              font-size: 10pt;

              .QToolTip
              background-color: #333;
              color: #fff;
              border: red solid 1px;
              opacity: 100;
              font-family : Consolas;
              font-size: 10pt;

              .QMessageBox
              background-color: #333;

              QStatusBar
              color: green;
              font-family : Consolas;
              font-size: 10pt;

              """)
              # violence
              self.label = QLabel('position of the mouse', self)
              self.label1 = QLabel('some words 一些单词', self)
              self.label2 = QLabel('digital clock', self)

              centralWidget = QWidget() # +++++
              self.setCentralWidget(centralWidget) # +++++

              grid = QGridLayout(centralWidget) # +++++ centralWidget

              # just blablabla
              grid.addWidget(self.label1, 0, 0)

              # Digital Clock
              grid.addWidget(self.label2, 1, 0)

              # Mouse tracking
              x, y = 0, 0
              self.text = "x: 0, y: 1".format(x, y)
              self.label = QLabel(self.text, self) # display the x and y coordinates of a mouse pointer in a label widget.
              grid.addWidget(self.label, 2, 0)
              self.setMouseTracking(True)

              # # push button widget and set a tooltip (useless)
              # #btn = QPushButton('Button', self)
              # #btn.setToolTip('This is a <b>QPushButton</b> widget for refresh')
              # #btn.resize(btn.sizeHint())
              # #hbox.addWidget(btn)

              # set the grid
              # self.setLayout(grid) # -----
              # set the window size using the setGeometry
              self.setGeometry(self.left, self.top, self.width, self.height)
              # set the window title
              self.setWindowTitle(self.title)
              self.show() # displays the widget on the screen

              def closeEvent(self, event): # Define behavior when clicking on the x button on the title bar
              reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
              if reply == QMessageBox.Yes:
              event.accept()
              else:
              event.ignore()

              def mouseMoveEvent(self, e): # for following the position of the mouse
              x, y = e.x(), e.y()
              text = "x: 0, y: 1".format(x, y)
              self.label.setText(text)

              def showTime(self): # for printing the time (format = 'hh:mm:ss')
              time = QTime.currentTime()
              text = time.toString('hh:mm:ss')
              self.label2.setText(text)

              if __name__ == '__main__':
              APP = QApplication(sys.argv)
              EXP = BgColorExperiment()
              sys.exit(APP.exec_()) # mainloop of the application


              enter image description here






              share|improve this answer

























                0












                0








                0







                void QMainWindow::setCentralWidget(QWidget *widget)



                Sets the given widget to be the main window's central widget.



                Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.



                import sys
                from PyQt5.QtWidgets import (QMainWindow, QWidget, QLabel, QApplication, QToolTip, QPushButton,
                QMessageBox, QGridLayout, QHBoxLayout, QVBoxLayout, QTextEdit)
                from PyQt5.QtGui import QIcon, QFont
                from PyQt5.QtCore import Qt, QTime

                class BgColorExperiment(QMainWindow):
                def __init__(self):
                super().__init__()
                # moves the widget to a position on the screen at x=300, y=300 coordinates
                self.left = 300
                self.top = 300
                # resizes the widget (250px wide and 150px high)
                self.width = 450
                self.height = 450
                # title of Widget shown in the titlebar
                self.title ='Suivi des BDs crées'
                # sets the application icon
                self.setWindowIcon(QIcon('suivibdicon.png'))
                # Set the status bar
                self.statusBar().showMessage('Engineered by idiots')
                # parent
                self.init_ui()

                def init_ui(self):
                # Artiste
                self.setObjectName('MainWidget')
                self.setStyleSheet("""
                #MainWidget
                background-color: #333;

                .QLabel
                color: green;
                background-color: #333;
                selection-color: yellow;
                selection-background-color: blue;
                font-family : Consolas;
                font-size: 10pt;

                .QToolTip
                background-color: #333;
                color: #fff;
                border: red solid 1px;
                opacity: 100;
                font-family : Consolas;
                font-size: 10pt;

                .QMessageBox
                background-color: #333;

                QStatusBar
                color: green;
                font-family : Consolas;
                font-size: 10pt;

                """)
                # violence
                self.label = QLabel('position of the mouse', self)
                self.label1 = QLabel('some words 一些单词', self)
                self.label2 = QLabel('digital clock', self)

                centralWidget = QWidget() # +++++
                self.setCentralWidget(centralWidget) # +++++

                grid = QGridLayout(centralWidget) # +++++ centralWidget

                # just blablabla
                grid.addWidget(self.label1, 0, 0)

                # Digital Clock
                grid.addWidget(self.label2, 1, 0)

                # Mouse tracking
                x, y = 0, 0
                self.text = "x: 0, y: 1".format(x, y)
                self.label = QLabel(self.text, self) # display the x and y coordinates of a mouse pointer in a label widget.
                grid.addWidget(self.label, 2, 0)
                self.setMouseTracking(True)

                # # push button widget and set a tooltip (useless)
                # #btn = QPushButton('Button', self)
                # #btn.setToolTip('This is a <b>QPushButton</b> widget for refresh')
                # #btn.resize(btn.sizeHint())
                # #hbox.addWidget(btn)

                # set the grid
                # self.setLayout(grid) # -----
                # set the window size using the setGeometry
                self.setGeometry(self.left, self.top, self.width, self.height)
                # set the window title
                self.setWindowTitle(self.title)
                self.show() # displays the widget on the screen

                def closeEvent(self, event): # Define behavior when clicking on the x button on the title bar
                reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                if reply == QMessageBox.Yes:
                event.accept()
                else:
                event.ignore()

                def mouseMoveEvent(self, e): # for following the position of the mouse
                x, y = e.x(), e.y()
                text = "x: 0, y: 1".format(x, y)
                self.label.setText(text)

                def showTime(self): # for printing the time (format = 'hh:mm:ss')
                time = QTime.currentTime()
                text = time.toString('hh:mm:ss')
                self.label2.setText(text)

                if __name__ == '__main__':
                APP = QApplication(sys.argv)
                EXP = BgColorExperiment()
                sys.exit(APP.exec_()) # mainloop of the application


                enter image description here






                share|improve this answer













                void QMainWindow::setCentralWidget(QWidget *widget)



                Sets the given widget to be the main window's central widget.



                Note: QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.



                import sys
                from PyQt5.QtWidgets import (QMainWindow, QWidget, QLabel, QApplication, QToolTip, QPushButton,
                QMessageBox, QGridLayout, QHBoxLayout, QVBoxLayout, QTextEdit)
                from PyQt5.QtGui import QIcon, QFont
                from PyQt5.QtCore import Qt, QTime

                class BgColorExperiment(QMainWindow):
                def __init__(self):
                super().__init__()
                # moves the widget to a position on the screen at x=300, y=300 coordinates
                self.left = 300
                self.top = 300
                # resizes the widget (250px wide and 150px high)
                self.width = 450
                self.height = 450
                # title of Widget shown in the titlebar
                self.title ='Suivi des BDs crées'
                # sets the application icon
                self.setWindowIcon(QIcon('suivibdicon.png'))
                # Set the status bar
                self.statusBar().showMessage('Engineered by idiots')
                # parent
                self.init_ui()

                def init_ui(self):
                # Artiste
                self.setObjectName('MainWidget')
                self.setStyleSheet("""
                #MainWidget
                background-color: #333;

                .QLabel
                color: green;
                background-color: #333;
                selection-color: yellow;
                selection-background-color: blue;
                font-family : Consolas;
                font-size: 10pt;

                .QToolTip
                background-color: #333;
                color: #fff;
                border: red solid 1px;
                opacity: 100;
                font-family : Consolas;
                font-size: 10pt;

                .QMessageBox
                background-color: #333;

                QStatusBar
                color: green;
                font-family : Consolas;
                font-size: 10pt;

                """)
                # violence
                self.label = QLabel('position of the mouse', self)
                self.label1 = QLabel('some words 一些单词', self)
                self.label2 = QLabel('digital clock', self)

                centralWidget = QWidget() # +++++
                self.setCentralWidget(centralWidget) # +++++

                grid = QGridLayout(centralWidget) # +++++ centralWidget

                # just blablabla
                grid.addWidget(self.label1, 0, 0)

                # Digital Clock
                grid.addWidget(self.label2, 1, 0)

                # Mouse tracking
                x, y = 0, 0
                self.text = "x: 0, y: 1".format(x, y)
                self.label = QLabel(self.text, self) # display the x and y coordinates of a mouse pointer in a label widget.
                grid.addWidget(self.label, 2, 0)
                self.setMouseTracking(True)

                # # push button widget and set a tooltip (useless)
                # #btn = QPushButton('Button', self)
                # #btn.setToolTip('This is a <b>QPushButton</b> widget for refresh')
                # #btn.resize(btn.sizeHint())
                # #hbox.addWidget(btn)

                # set the grid
                # self.setLayout(grid) # -----
                # set the window size using the setGeometry
                self.setGeometry(self.left, self.top, self.width, self.height)
                # set the window title
                self.setWindowTitle(self.title)
                self.show() # displays the widget on the screen

                def closeEvent(self, event): # Define behavior when clicking on the x button on the title bar
                reply = QMessageBox.question(self, 'Message', "Are you sure to quit?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                if reply == QMessageBox.Yes:
                event.accept()
                else:
                event.ignore()

                def mouseMoveEvent(self, e): # for following the position of the mouse
                x, y = e.x(), e.y()
                text = "x: 0, y: 1".format(x, y)
                self.label.setText(text)

                def showTime(self): # for printing the time (format = 'hh:mm:ss')
                time = QTime.currentTime()
                text = time.toString('hh:mm:ss')
                self.label2.setText(text)

                if __name__ == '__main__':
                APP = QApplication(sys.argv)
                EXP = BgColorExperiment()
                sys.exit(APP.exec_()) # mainloop of the application


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 23:39









                S. NickS. Nick

                3,486247




                3,486247















                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto