PyQT5 : Qlabel overlay eachother even after using grid [duplicate]
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
python python-3.x pyqt pyqt5 qlabel
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.
add a comment |
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
python python-3.x pyqt pyqt5 qlabel
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.
add a comment |
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
python python-3.x pyqt pyqt5 qlabel
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
python python-3.x pyqt pyqt5 qlabel
edited Nov 15 '18 at 0:13
eyllanesc
84.3k103562
84.3k103562
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Nov 14 '18 at 23:39
S. NickS. Nick
3,890258
3,890258
add a comment |
add a comment |