How to add QVBoxLayout. around Qlabel and Qpushbutton?
I have this how can I add QVBoxLayout to make something like that
I have this code :
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication , QMainWindow , QPushButton ,
QToolTip , QLabel
import sys
class Window (QMainWindow):
def __init__(self):
super().__init__()
self.title = "pyQt5"
self.top = 100
self.left = 100
self.width = 680
self.height= 500
button = QPushButton("print", self)
button.move(200,200)
lb = QLabel('Hi', self)
lb.move(200,100)
self.s()
def s(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top,self.left,self.width,self.height)
self.show()
if __name__ == '__main__':
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
here image explains what I mean:
Try it:
import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import (QApplication , QMainWindow , QPushButton ,
QToolTip , QLabel, QVBoxLayout, QWidget)
from PyQt5.QtCore import Qt
class Window (QMainWindow):
def __init__(self):
super().__init__()
self.title = "pyQt5"
self.top = 100
self.left = 100
self.width = 680
self.height= 500
self.main_widget = QWidget()
self.setCentralWidget(self.main_widget)
layout = QVBoxLayout(self.main_widget)
button = QPushButton("print", self)
button.setStyleSheet('background-color:blue; color:white; font-size:24px;')
lb = QLabel('Hello', self)
lb.setStyleSheet('background-color:green; color:white; font-size:24px;')
layout.addStretch(1)
layout.addWidget(lb)
layout.addStretch(1)
layout.addWidget(button)
layout.addStretch(1)
layout.setAlignment(Qt.AlignCenter)
self.s()
def s(self):
self.setWindowTitle(self.title)
self.setGeometry(self.top,self.left,self.width,self.height)
self.show()
if __name__ == '__main__':
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
Related
I have had this happening multiple times. When I create a custom widget with an image in it all child widgets get seperated. How do I prevent that?
Full code that has the same outcome:
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sys
class CustomWidget(QWidget):
def __init__(self):
super(CustomWidget, self).__init__()
self.layout = QHBoxLayout()
self.setLayout(self.layout)
self.setStyleSheet(r'QWidget {background-color:#353634;}')
self.name_label = QLabel('name')
self.qty_label = QLabel('999')
self.image_pix = QPixmap(IMAGEPATH)
self.image_pix = self.image_pix.scaled(48, 48)
self.icon = QLabel()
self.icon.setPixmap(self.image_pix)
self.layout.addWidget(self.icon)
self.layout.addWidget(self.name_label)
self.layout.addWidget(self.qty_label)
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.width, self.height = 425,350
self.resize(self.width, self.height)
custom_widget = CustomWidget()
self.setCentralWidget(custom_widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec()
app.quit()
I am dealing with the following problem, while I am having multiple windows open, i would like to build a function linked to a button to bring to the front the Main window.
Thank you in advance.
import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton,
QLabel)
class Window2(QMainWindow): # <===
def __init__(self):
super().__init__()
self.setWindowTitle("Window 2")
self.pushButton = QPushButton("Back to window1", self)
self.pushButton.clicked.connect(self.window1)
def window1(self): # <===
pass;
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "First Window"
self.top = 100
self.left = 100
self.width = 680
self.height = 500
self.pushButton = QPushButton("Go to window 2 ", self)
self.pushButton.move(275, 200)
self.label = QLabel("window 1", self)
self.label.move(285, 175)
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.pushButton.clicked.connect(self.window2) # <===
def window2(self): # <===
self.w = Window2()
self.w.show()
def main():
app = QApplication(sys.argv)
window = Window()
window.show()
#app.exec_()
exit(app.exec_())
if __name__=='__main__':
main()
Regards
I am expecting a function to call back the widget "Window"
You could emit a signal from your second window that your fist window listens for, and calls .raise_() when triggered.
Update: Added a call to activateWindow in the first windows callback. thanks #musicmante
For example:
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import pyqtSignal # import signal
from PyQt5.QtWidgets import (QApplication, QMainWindow, QPushButton,
QLabel)
class Window2(QMainWindow):
unfocus = pyqtSignal() # create signal
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setWindowTitle("Window 2")
self.pushButton = QPushButton("Back to window1", self)
# button press emits signal
self.pushButton.clicked.connect(self.unfocus.emit)
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.title = "First Window"
self.top = 100
self.left = 100
self.width = 680
self.height = 500
self.pushButton = QPushButton("Go to window 2 ", self)
self.pushButton.move(275, 200)
self.label = QLabel("window 1", self)
self.label.move(285, 175)
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.pushButton.clicked.connect(self.window2) # <===
def window2(self): # <===
self.w = Window2()
self.w.unfocus.connect(self.bring_to_top) # listen for signal and raise_ to top focus
self.w.show()
def bring_to_top(self):
self.activateWindow()
self.raise_()
def main():
app = QApplication(sys.argv)
window = Window()
window.show()
#app.exec_()
exit(app.exec_())
if __name__=='__main__':
main()
I'm new to Qt5, I have a simple QGridLayout layout mask .
I want to create a windows with the widget resize with resize of window
this is the code
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog ,QVBoxLayout,QGroupBox,QGridLayout
class MainWindow(QtWidgets.QMainWindow, QtWidgets.QFileDialog, QtWidgets.QLineEdit):
def __init__(self):
super().__init__()
self.title = "Calcolo Hash"
self.top = 100
self.left = 100
self.width = 800
self.height = 330
self.InitWindow()
def InitWindow(self):
self.setWindowIcon(QtGui.QIcon("icona_aprie.png"))
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.creamaschera()
self.show()
def creamaschera(self):
print ("creazione maschera")
layout = QtWidgets.QGridLayout()
self.txtcartella = QtWidgets.QLineEdit()
self.lblprova = QtWidgets.QLabel("Please enter new name:")
# self.txtcartella.setGeometry(QtCore.QRect(10, 10, 301, 20))
# self.txtcartella.setObjectName("txtcartella")
layout.addWidget(self.lblprova,0,0)
layout.addWidget(self.txtcartella,0,1)
self.setLayout(layout)
# self.horizontalGroupBox.setLayout(layout)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
#w.show()
sys.exit(app.exec_())
but when I run the mask is empy.
I make the base with Qt5 designer and convert it to python. I want to refactor the class in a best workout.
Where is the error?
You should setLayout in a widget rather than setting it to the MainWindow since you are using the MainWindow class itself and while accessing the methods and properties of the class MainWindow you can be more specific
import sys
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import (QLineEdit, QMainWindow, QWidget,
QGridLayout, QLabel, QApplication)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.title = "Calcolo Hash"
self.top = 100
self.left = 100
self.width = 800
self.height = 330
self.InitWindow()
def InitWindow(self):
self.setWindowIcon(QtGui.QIcon("icona_aprie.png"))
self.setWindowTitle(self.title)
self.setGeometry(self.top, self.left, self.width, self.height)
self.creamaschera()
def creamaschera(self):
print("creazione maschera")
Layout = QGridLayout()
self.txtcartella = QLineEdit()
self.lblprova = QLabel("Enter Your Name")
self.lblprova.setGeometry(QtCore.QRect(15, 15, 301, 20))
self.txtcartella.setObjectName("txtcartella")
Layout.addWidget(self.lblprova, 0, 0)
Layout.addWidget(self.txtcartella, 0, 1)
# Widget to setLayout in it since you are using MainWindow as an Class
widget = QWidget()
widget.setLayout(Layout)
# SetCentralWidget without this widget won't be placed
self.setCentralWidget(widget)
# self.horizontalGroupBox.setLayout(layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
MainWindow = MainWindow()
MainWindow.show()
sys.exit(app.exec_())
I have a Python app that uses PyQt5 for it's GUI. I have a Tab Widget in it, and I want to add and remove tabs outside of window class. Something like:
Tabs.addTab("name")
How do I do that?
Here is my code:
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTabWidget ,QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'Test'
self.left = 0
self.top = 0
self.width = 500
self.height = 500
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.table_widget = MyTableWidget(self)
self.setCentralWidget(self.table_widget)
self.show()
class MyTableWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.layout = QVBoxLayout(self)
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tabs.resize(300,200)
self.tabs.addTab(self.tab1, "Tab 1")
self.tabs.addTab(self.tab2, "Tab 2")
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Thank you for your help!
It does not matter if you are going to remove the tab within the class or outside of it but you have to use the QTabWidget object, for example in your case if you want to add a tab from the "App" class then you must do it through the object "table_widget" whose attribute is "tabs" which is the QTabWidget:
class App(QMainWindow):
def __init__(self):
super().__init__()
# ...
self.table_widget.tabs.addTab(QWidget(), "name") # <--- add tab
self.table_widget.tabs.removeTab(0) # <--- remove tab
I'm using Ubuntu 14.04 and python 3.4.3 with PyQt 5.5.1. I've faced with a following problem,when I press Ctrl+Q noting happens.Coud you explain why does it happen and how to solve this problem. My code is below:
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction, QLineEdit, QMessageBox, QLabel
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot, Qt
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'PyQt5 textbox - pythonspot.com'
self.left = 10
self.top = 10
self.width = 400
self.height = 140
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('File')
editMenu = mainMenu.addMenu('Edit')
viewMenu = mainMenu.addMenu('View')
searchMenu = mainMenu.addMenu('Search')
toolsMenu = mainMenu.addMenu('Tools')
helpMenu = mainMenu.addMenu('Help')
exitButton = QAction(QIcon('exit24.png'), 'Exit', self)
exitButton.setShortcut('CTRL+Q')
exitButton.setStatusTip('Exit application')
exitButton.triggered.connect(self.close)
fileMenu.addAction(exitButton)
self.show()
#pyqtSlot()
def on_click(self):
textboxValue = self.textbox.text()
QMessageBox.question(self, 'Message - pythonspot.com', "You typed: " + textboxValue, QMessageBox.Ok, QMessageBox.Ok)
self.textbox.setText("")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())