i'm pretty new to python and PyQt5.
My goal is to use a "Push Button" to move to the next widget in the "Stacked Layout". However, it's not being responsive, and it appears that it won't enter the function in order to move on to the next widget.
I have no idea wether its the inheritance i've messed up on, or something else. Any guidance is really appreciated. Thank you!
from PyQt5 import QtCore, QtGui, QtWidgets
class Menu(QtWidgets.QWidget):
def setupUI(self, Main):
Main.setObjectName("Main")
Main.setFixedSize(900, 500)
self.width = 900
self.height = 500
self.setFixedSize(self.width, self.height)
self.menu = QtWidgets.QStackedLayout()
self.welcomeMenu = QtWidgets.QWidget()
self.mainMenu = QtWidgets.QWidget()
self.welcomeUi()
self.menuUi()
self.menu.addWidget(self.welcomeMenu)
self.menu.addWidget(self.mainMenu)
def welcomeUi(self):
#Button for entering portal
self.entrBtn = QtWidgets.QPushButton(self.welcomeMenu)
self.entrBtn.setGeometry(QtCore.QRect(25,150,200,50))
self.entrBtn.setText("To the menu screen!")
#Having welcome text
self.welcomeText = QtWidgets.QLabel(self.welcomeMenu)
self.welcomeText.setGeometry(QtCore.QRect(30, 120, 480, 200))
self.welcomeText.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.welcomeText.setText("Welcome!")
def menuUi(self):
self.text = QtWidgets.QLabel(self.mainMenu)
self.text.setGeometry(QtCore.QRect(30, 120, 480, 200))
self.text.setText("test")
class Main(QtWidgets.QMainWindow, Menu):
def __init__(self):
super(Main, self).__init__()
self.setupUI(self)
self.entrBtn.clicked.connect(self.menuWindow)
def menuWindow(self):
self.menu.setCurrentWidget(self.mainMenu)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
m = Main()
sys.exit(app.exec_())
The problem is simple: QLabel is on top of the button so it will block all mouse events. The simple solution is to put QPushButton on QLabel using raise_():
# ...
self.welcomeText.setText("Welcome!")
self.entrBtn.raise_()
But that solves the superficial problem, you have other bigger problem: You should not inherit from 2 QWidget, also the Main is not shown. It is better to rewrite the code as follows:
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.welcomeMenu = QtWidgets.QWidget()
self.mainMenu = QtWidgets.QWidget()
self.fill_welcomeUi()
self.fill_menuUi()
self.stacked_lay = QtWidgets.QStackedLayout()
self.stacked_lay.addWidget(self.welcomeMenu)
self.stacked_lay.addWidget(self.mainMenu)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
central_widget.setLayout(self.stacked_lay)
self.entrBtn.clicked.connect(self.menuWindow)
self.resize(640, 480)
def menuWindow(self):
self.stacked_lay.setCurrentWidget(self.mainMenu)
def fill_welcomeUi(self):
#Button for entering portal
self.entrBtn = QtWidgets.QPushButton(self.welcomeMenu)
self.entrBtn.setGeometry(QtCore.QRect(25,150,200,50))
self.entrBtn.setText("To the menu screen!")
#Having welcome text
self.welcomeText = QtWidgets.QLabel(self.welcomeMenu)
self.welcomeText.setGeometry(QtCore.QRect(30, 120, 480, 200))
self.welcomeText.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
self.welcomeText.setText("Welcome!")
self.entrBtn.raise_()
def fill_menuUi(self):
self.text = QtWidgets.QLabel(self.mainMenu)
self.text.setGeometry(QtCore.QRect(30, 120, 480, 200))
self.text.setText("test")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
m = MainWindow()
m.show()
sys.exit(app.exec_())
Related
In PyQt5, I've wrote a GUI. Basically, when a button is pressed, it open a new window, where choose an item from a list. What I want is that after you close the new window, the item you chose appears as text on the first window. It's a hard to explain.
This is the code:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5 import QtGui
class Add(QtWidgets.QMainWindow):
def __init__(self):
super(Add, self).__init__()
#Set The UI
self.initUI()
#Set The GUI Position And Size
self.setGeometry(1050, 500, 400, 50)
#Set The GUI Title
self.setWindowTitle("Add")
def initUI(self):
Central = QtWidgets.QWidget(self)
self.setCentralWidget(Central)
self.deckButton = QtWidgets.QPushButton(self)
self.deckButton.setText("Choose")
self.deckButton.clicked.connect(self.open_deck_browser)
hbox = QtWidgets.QHBoxLayout()
hbox.addWidget(self.deckButton, 1)
Central.setLayout(hbox)
def open_deck_browser(self):
self.w = SetDeck()
self.w.show()
class SetDeck(QtWidgets.QMainWindow):
def __init__(self):
super(SetDeck, self).__init__()
#Set The UI
self.initUI()
#Set The GUI Position And Size
self.setGeometry(200, 200, 800, 640)
#Set The GUI Title
self.setWindowTitle("Choose Deck")
def initUI(self):
widAddToDeckWindow = QtWidgets.QWidget(self)
self.setCentralWidget(widAddToDeckWindow)
#Create The List Widget
self.deckList = QtWidgets.QListWidget()
self.deckList.insertItem(0, "Hello")
self.deckList.insertItem(1, "Hi")
self.deckList.insertItem(2, "Hello There")
self.deckList.item(0).setSelected(True)
self.deckList.itemSelectionChanged.connect(self.show_List)
print([x.row() for x in self.deckList.selectedIndexes()])
#Create The Select Deck Button
self.selectDeck = QtWidgets.QPushButton(self)
self.selectDeck.setText("Choose")
hboxCreateBottomButtons = QtWidgets.QHBoxLayout()
hboxCreateBottomButtons.addStretch()
hboxCreateBottomButtons.addWidget(self.selectDeck)
#Create The Main VBox
vboxMain = QtWidgets.QVBoxLayout()
vboxMain.addWidget(self.deckList)
vboxMain.addLayout(hboxCreateBottomButtons)
widAddToDeckWindow.setLayout(vboxMain)
def show_List(self):
print(repr(self.deckList.selectedItems()[0].text()))
def window():
app = QtWidgets.QApplication(sys.argv)
win = Add()
win.show()
sys.exit(app.exec_())
window()
I've tried using global variables, but it didn't work.
First of all, I recommend you improve your style when naming variables as they make reading easier, for example that the class name are nouns and not a verb.
Getting to the bottom of the problem, never use (or try to use) global variables as they cause silent bugs if there is a better option and you don't understand how it works. In the case that you want a window where you ask the user to provide information on how to select an option that will then be used in the main window then it is advisable to use a QDialog. In the following example, this is done and the "Choose" button is linked to the accept slot so that it closes the window, and that information can be used to know that the "x" button of the window was not closed. Also I have created a property that has the selected text.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.initUI()
self.setGeometry(1050, 500, 400, 50)
self.setWindowTitle("Add")
def initUI(self):
central = QtWidgets.QWidget(self)
self.setCentralWidget(central)
self.deckButton = QtWidgets.QPushButton()
self.deckButton.setText("Choose")
self.deckButton.clicked.connect(self.open_deck_browser)
box = QtWidgets.QVBoxLayout(central)
box.addWidget(self.deckButton)
self.label = QtWidgets.QLabel()
box.addWidget(self.label)
self.label.hide()
def open_deck_browser(self):
dialog = DeckDialog()
if dialog.exec_() == QtWidgets.QDialog.Accepted:
self.label.show()
self.label.setText(dialog.selected_text)
class DeckDialog(QtWidgets.QDialog):
def __init__(self):
super(DeckDialog, self).__init__()
self.initUI()
self.setGeometry(200, 200, 800, 640)
self.setWindowTitle("Choose Deck")
def initUI(self):
self.deckList = QtWidgets.QListWidget()
self.deckList.insertItem(0, "Hello")
self.deckList.insertItem(1, "Hi")
self.deckList.insertItem(2, "Hello There")
self.deckList.item(0).setSelected(True)
self.selectDeck = QtWidgets.QPushButton(self)
self.selectDeck.setText("Choose")
hboxCreateBottomButtons = QtWidgets.QHBoxLayout()
hboxCreateBottomButtons.addStretch()
hboxCreateBottomButtons.addWidget(self.selectDeck)
vboxMain = QtWidgets.QVBoxLayout(self)
vboxMain.addWidget(self.deckList)
vboxMain.addLayout(hboxCreateBottomButtons)
self.selectDeck.clicked.connect(self.accept)
#property
def selected_text(self):
items = self.deckList.selectedItems()
if items:
return items[0].text()
return ""
def main():
app = QtWidgets.QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I want to set the background for text, which means that I want to set the color of the rectangle contains the text. I have tested QPainter.setBackground, but it do not work. This is my code:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MyLabel(QLabel):
def __init__(self):
super(MyLabel, self).__init__()
self.setMinimumHeight(200)
self.setMinimumWidth(200)
def paintEvent(self, QPaintEvent):
super(MyLabel, self).paintEvent(QPaintEvent)
pos = QPoint(50, 50)
painter = QPainter(self)
brush = QBrush()
brush.setColor(QColor(255,0,0))
painter.setBackgroundMode(Qt.OpaqueMode)
painter.setBackground(brush)
painter.drawText(pos, 'hello,world')
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
layout = QHBoxLayout(self)
self.label = MyLabel()
layout.addWidget(self.label)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
What I want is:
Thanks for any help.
It is not necessary to implement a personalized QLabel, it is enough to set the background color through Qt Style Sheet, also do not use a layout if you want to establish a certain position
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.label = QtWidgets.QLabel("hello,world", self)
self.label.adjustSize()
self.label.setStyleSheet(
"background-color: {};".format(QtGui.QColor(255, 0, 0).name())
)
self.label.move(QtCore.QPoint(50, 50))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())
I clicked the button Platform Type, but Ui_Form doesn't show
I tried exec_() but Process finished with exit code -1073740791 (0xC0000409)
I want to open new QWidget window when clicked the button
Details: In Python, I want to make one more window when I press the button of the first widget. I tried to follow the other contents, but the program terminated with an error code. I would appreciate if you could let me know what the problem is.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Main_Widget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self._mutex = QtCore.QThread()
self.setupUi(Main_Widget)
def setupUi(self, Main_Widget):
Main_Widget.setObjectName('Main_Widget')
Main_Widget.resize(1272, 640)
self.All_GroupBox = QtWidgets.QGroupBox(Main_Widget)
self.All_GroupBox.setGeometry(QtCore.QRect(20, 10, 1231, 611))
font = QtGui.QFont()
self.All_GroupBox.setFont(font)
self.All_GroupBox.setObjectName('All_GroupBox')
self.verticalLayoutWidget = QtWidgets.QWidget(self.All_GroupBox)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 261, 331))
self.verticalLayoutWidget.setObjectName('verticalLayoutWidget')
self.Button_VerticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.Button_VerticalLayout.setContentsMargins(0, 0, 0, 0)
self.Button_VerticalLayout.setObjectName('Button_VerticalLayout')
self.PlatformType_Button = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.PlatformType_Button.setMinimumSize(QtCore.QSize(0, 45))
self.PlatformType_Button.setObjectName('PlatformType_Button')
self.Button_VerticalLayout.addWidget(self.PlatformType_Button)
def PlatformType_Clicked(self):
dialog = Ui_Form(self)
self.dialogs.append(dialog)
dialog.show()
dialog.exec_()
class Ui_Form(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Ui_Form, self).__init__(parent)
def setupUi(self, Form):
Form.setObjectName('Form')
Form.resize(422, 190)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Main_Widget = QtWidgets.QWidget()
ui = Ui_Main_Widget()
Main_Widget.show()
app.exec_()
Try it:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Main_Widget(QtWidgets.QWidget):
# def __init__(self):
# super().__init__()
# self._mutex = QtCore.QThread()
# self.setupUi(Main_Widget)
def setupUi(self, Main_Widget):
Main_Widget.setObjectName('Main_Widget')
Main_Widget.resize(1272, 640)
self.All_GroupBox = QtWidgets.QGroupBox(Main_Widget)
self.All_GroupBox.setGeometry(QtCore.QRect(20, 10, 1231, 611))
font = QtGui.QFont()
self.All_GroupBox.setFont(font)
self.All_GroupBox.setObjectName('All_GroupBox')
self.verticalLayoutWidget = QtWidgets.QWidget(self.All_GroupBox)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(20, 30, 261, 331))
self.verticalLayoutWidget.setObjectName('verticalLayoutWidget')
self.Button_VerticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.Button_VerticalLayout.setContentsMargins(0, 0, 0, 0)
self.Button_VerticalLayout.setObjectName('Button_VerticalLayout')
self.PlatformType_Button = QtWidgets.QPushButton("PlatformType_Button", self.verticalLayoutWidget)
self.PlatformType_Button.setMinimumSize(QtCore.QSize(0, 45))
self.PlatformType_Button.setObjectName('PlatformType_Button')
# +++
self.PlatformType_Button.clicked.connect(Main_Widget.PlatformType_Clicked) # +++
self.Button_VerticalLayout.addWidget(self.PlatformType_Button)
# def PlatformType_Clicked(self):
# dialog = Ui_Form(self)
# self.dialogs.append(dialog)
# dialog.show()
# dialog.exec_()
class Ui_Form(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Ui_Form, self).__init__(parent)
def setupUi(self, Form):
Form.setObjectName('Form')
Form.resize(422, 190)
class MyWindow(QtWidgets.QWidget): # +++
def __init__(self):
super().__init__()
self.ui = Ui_Main_Widget()
self.ui.setupUi(self)
# +++
def PlatformType_Clicked(self):
self.dialog = Ui_Form() # --- self
# self.dialogs.append(dialog)
self.dialog.show()
# self.dialog.exec_()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = MyWindow()
w.show()
sys.exit(app.exec_())
hide() and show() method you can use multiple dialog what ever you want,...
def PlatformType_Clicked(self):
dialog.hide()
dialog1.show()
I decided to write a visual form for my script.
The idea is to have a button that will add new tabs to QTabWidget. It does not work and I can not find a good example. I use PyQt5. Here's a piece of what I've tried:
import sys
from PyQt5.QtGui import QIcon
from PyQt5 import QtCore, QtWidgets
class mainForm(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.runUi()
def runUi(self):
self.resize(250, 150)
self.move(300, 300)
self.setWindowTitle('Let\'s Rock!')
self.setWindowIcon(QIcon('icon.png'))
self.setMaximumSize(QtCore.QSize(560, 522))
self.setMinimumSize(QtCore.QSize(560, 522))
groupBoxGD = QtWidgets.QGroupBox('Соединение с ГД', self)
groupBoxGD.setGeometry(QtCore.QRect(10, 10, 541, 151))
hrLWGDLink = QtWidgets.QWidget(groupBoxGD)
hrLWGDLink.setGeometry(QtCore.QRect(10, 10, 521, 31))
hrLGD = QtWidgets.QHBoxLayout(hrLWGDLink)
hrLGD.setContentsMargins(0, 0, 0, 0)
btnAddTab = QtWidgets.QPushButton(hrLWGDLink)
btnAddTab.setText('Add tab')
hrLGD.addWidget(btnAddTab)
tabWidget = QtWidgets.QTabWidget(groupBoxGD)
tabWidget.setGeometry(QtCore.QRect(10, 170, 541, 351))
btnAddTab.clicked.connect(self.addProjectTab)
self.show()
def addProjectTab(self):
tab = QtWidgets.QWidget()
#how add tab at this line?
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
ui = mainForm()
sys.exit(app.exec_())
You have to use the addTab() function, but to do so from another class the QTabWidget object must be a member of the class. Also I made some changes in the design because the button was on the QTabWidget, covering the tabs.
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class mainForm(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.runUi()
def runUi(self):
self.resize(250, 150)
self.move(300, 300)
self.setWindowTitle('Let\'s Rock!')
self.setWindowIcon(QtGui.QIcon('icon.png'))
self.setMaximumSize(QtCore.QSize(560, 522))
self.setMinimumSize(QtCore.QSize(560, 522))
layout = QtWidgets.QVBoxLayout(self)
groupBoxGD = QtWidgets.QGroupBox('Соединение с ГД', self)
layout2 = QtWidgets.QVBoxLayout(groupBoxGD)
hrLWGDLink = QtWidgets.QWidget(groupBoxGD)
hrLGD = QtWidgets.QVBoxLayout(hrLWGDLink)
hrLGD.setContentsMargins(0, 0, 0, 0)
btnAddTab = QtWidgets.QPushButton(hrLWGDLink)
btnAddTab.setText('Add tab')
hrLGD.addWidget(btnAddTab)
self.tabWidget = QtWidgets.QTabWidget(hrLWGDLink)
hrLGD.addWidget(self.tabWidget)
layout2.addWidget(hrLWGDLink)
layout.addWidget(groupBoxGD)
btnAddTab.clicked.connect(self.addProjectTab)
def addProjectTab(self):
tab = QtWidgets.QWidget()
self.tabWidget.addTab(tab, "tab")
app = QtWidgets.QApplication(sys.argv)
w = mainForm()
w.show()
sys.exit(app.exec_())
Screenshot:
I have a button and a text label. Each time the button is pressed, i would like text placed from a line edit to be placed onto the window. So far I can only get one text to draw onto the window, even if I create another textlabel. Ive tried seeting a click count determining how many times a user has clicked a button but this doesnt work either. Heres what I have so far, any suggestions?
import sys
import os
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtGui import QApplication
class Window(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.centralWidget = QWidget(self)
self.setCentralWidget(self.centralWidget)
self.setGeometry(450,100,350,680)
self.btn1 = QPushButton("Enter", self.centralWidget)
self.btn1.setGeometry(10,50,150, 20)
self.btn1.clicked.connect(self.enter)
self.edit = QtGui.QLineEdit(self)
self.edit.setGeometry(10, 10, 150, 20)
self.label = QtGui.QLabel(self)
self.label.setGeometry(240, 170,150, 20)
def enter(self):
self.label.setText(self.edit.text())
def main(args):
global app
app = App(args)
app.exec_()
class App(QApplication):
def __init__(self, *args):
QApplication.__init__(self, *args)
self.main = Window()
self.connect(self, SIGNAL("lastWindowClosed()"), self.byebye )
self.main.show()
def byebye( self ):
self.exit(0)
if __name__ == "__main__":
main(sys.argv)
There are a few problems with your example code, the main one being that you are trying to manually arrange the widgets, rather than using a layout.
It's hard to tell from your question exactly what you expect the output to be, so I've assumed you want the text from line-edit to be appended to the label, so that you end up with a series of lines.
Here's a simplified version of your example that hopefully does what you want:
from PyQt4 import QtCore, QtGui
class Window(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.btn1 = QtGui.QPushButton("Enter", self)
self.btn1.clicked.connect(self.enter)
self.edit = QtGui.QLineEdit(self)
self.label = QtGui.QLabel(self)
self.label.setAlignment(
QtCore.Qt.AlignTop | QtCore.Qt.AlignLeft)
widget = QtGui.QWidget(self)
layout = QtGui.QVBoxLayout(widget)
layout.addWidget(self.edit)
layout.addWidget(self.btn1)
layout.addWidget(self.label)
self.setCentralWidget(widget)
def enter(self):
text = self.edit.text()
if text:
self.label.setText('%s\n%s' % (self.label.text(), text))
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(450, 100, 350, 680)
window.show()
sys.exit(app.exec_())