QStackedWidget Window Communication - python

I Have a main.py file and two QMainWindow.py files as well. When a PushButton is clicked on the second QMainWindow.py file it should open a first QStackedWidget, and on the first QStackedWidget when a PushButton is clicked it should show a second QStackedWidget window.
main.py
from PyQt5 import QtWidgets, QtCore
from mainmenu import MainWindow
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
mainMenu.py
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Reg_Emp_Button.clicked.connect(self.manage_reg_Employees)
self.ui.Report_Button.clicked.connect(self.Employee_Report)
def manage_reg_Employees(self):
self.hide()
self.employeeWindow = EmployeeWindow(self)
self.employeeWindow.show()
def Employee_Report(self):
self.hide()
self.chartWindow = ChartWindow(self)
self.chartWindow.show()
ManageEmployee.py
class EmployeeWindow(QtWidgets.QMainWindow):
def __init__(self, mainMenu):
super(EmployeeWindow, self).__init__()
self.mainMenu = mainMenu
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Back_Button.clicked.connect(self.Back_To_MainMenu)
self.ui.Add_Employee_Button_2.clicked.connect(self.Add_New_Employee)
def Back_To_MainMenu(self):
self.hide()
self.mainMenu.show()

Related

PyQt QMainwindow call QListWidget from a different file

I have a problem with PyQt. For several reasons, I have to separate the QListWidget from the main (qt.py) file. When I run my code only the "mainwindow" shows. It's like the list is didn't even called:
qt.py:
from qt_child import *
class mainwindow(QMainWindow):
def __init__(self):
super(mainwindow, self).__init__()
self.setGeometry(100,100,500,500)
self.lw = ListWidget()
def window():
app = QApplication(sys.argv)
w = mainwindow()
w.setWindowTitle("PyQt Main")
w.show()
sys.exit(app.exec_())
if __name__ == '__main__':
window()
qt_child.py:
import sys
from PyQt5 import QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QMainWindow, QListWidget
class ListWidget(QListWidget):
def __init__(self):
super(ListWidget, self).__init__()
self.resize(300,100)
self.addItem("Item 1")
self.addItem("Item 2")
self.addItem("Item 3")
self.addItem("Item 4")
change these rows
self.lw = ListWidget()
def __init__(self):
super(ListWidget, self).__init__()
to
self.lw = ListWidget(self)
def __init__(self, parent=None):
super(ListWidget, self).__init__(parent)

How to change PushButton border radius

I want to change border radius of my PushButton lesson1
Tried to create a class Button with setStyleSheet in __init__ and create an object.
import sys
from pyto import *
from PyQt5 import QtCore, QtGui, QtWidgets
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.lesson1 = Button()
class Button(QtWidgets.QPushButton):
def __init__(self, parent = None):
super(Button, self).__init__(parent)
self.setStyleSheet('border-radius: 15px;')
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
myapp = MyWin()
myapp.show()
sys.exit(app.exec_())
No errors, but setStyleSheet doesn't work.
The code self.ui.lesson1 = Button() does not replace the self.ui.lesson1 created by Qt Designer, it only causes the new Button() to be assigned to the name self.ui.lesson1. So if you want to set the stylesheet it is not necessary to create another class:
# ...
class MyWin(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# self.ui.lesson1 = Button()
self.ui.lesson1.setStyleSheet('border-radius: 15px; background-color: red;')
# ...
I have added the background color to make the radius visible.

Compose a PyQt5 UI from multiple classes

I have an UI designed in QT Designer. It is formed from three tabs:
1 - Test; 2 - Train Haar; 3 - Train Hog;
In every tabs I have some buttons, or some lines, or some widgets, but when I create the code to add functions to those buttons or widgets I want to have 3 classes for every tabs, one class only for first tab, one class only for second tab and so on..
And a fourth class who call all three classes and compose my UI. I do not know how to do this, I need every classes to inherit from QMainWindow? I need to setupUi in every class?
This is my current code:
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from Qt_designer_UI import Ui_MainWindow
class Test(QtWidgets.QMainWindow):
def __init__(self):
super(Test, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Train_Haar(QtWidgets.QMainWindow):
def __init__(self):
super(Train_Haar, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Train_HOG(QtWidgets.QMainWindow):
def __init__(self):
super(Train_HOG, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
class Compose_UI(QtWidgets.QMainWindow):
def __init__(self):
super(Compose_UI, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
Test()
Train_Haar()
Train_HOG
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Compose_UI()
window.show()
sys.exit(app.exec_())
I need every classes to inherit from QMainWindow?
No, it is not necessary since it is a widget that will be inside the tabs, and that widget can be of any type.
I need to setupUi in every class?
It is only obligatory to call setupUi if you use a generated class with the help of pyuic and Qt Designer, in your case the widgets that are in each tab are generated with Qt Designer? I see that I do not
Keeping in mind that your .ui is
a possible solution is:
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from Qt_designer_UI import Ui_MainWindow
class Test(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Test, self).__init__(parent)
# for testing
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(QtWidgets.QPushButton("Test"))
class Train_Haar(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Train_Haar, self).__init__(parent)
# for testing
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(QtWidgets.QPushButton("Train_Haar"))
class Train_HOG(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Train_HOG, self).__init__(parent)
# for testing
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(QtWidgets.QPushButton("Train_HOG"))
class Compose_UI(QtWidgets.QMainWindow):
def __init__(self):
super(Compose_UI, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
test = Test()
train_haar = Train_Haar()
train_hog = Train_HOG()
for w, tab in zip(
(test, train_haar, train_hog), (self.ui.tab1, self.ui.tab2, self.ui.tab3)
):
lay = QtWidgets.QVBoxLayout(tab)
lay.addWidget(w)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Compose_UI()
window.show()
sys.exit(app.exec_())

Open a file from main window to a new window in PyQt5 (in different files)

I have two files, one for my main window, which has one image and one button and one for a new window. What I want it to do is that when I push the button from my main window, it lets me load a file and show it in a TextEdit widget in the new window
so here I have the files I'm using:
MainWindow.py
import sys
import os
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtWidgets import QPushButton, QVBoxLayout, QTextEdit, QHBoxLayout, QLabel, QMainWindow, QAction, QFileDialog
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.img = QLabel()
self.relleno=QLabel()
self.btn_load = QPushButton('Load')
self.width = 400
self.height = 150
self.init_ui()
def init_ui(self):
self.img.setPixmap(QtGui.QPixmap("someimage.png"))
h_layout = QHBoxLayout()
v_layout = QVBoxLayout()
h_final = QHBoxLayout()
h_layout.addWidget(self.img)
v_layout.addWidget(self.btn_load)
h_final.addLayout(h_layout)
h_final.addLayout(v_layout)
self.btn_load.clicked.connect(self.loadafile)
self.setLayout(h_final)
self.setWindowTitle('This is main window')
self.setGeometry(600,150,self.width,self.height)
self.show()
def loadafile(self):
filename = QFileDialog.getOpenFileName(self, 'Open File', os.getenv('HOME'))
with open(filename[0], 'r') as f:
file_text = f.read()
return file_text
def main():
app = QApplication(sys.argv)
main = Window()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
NewWindow.py
import os
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTextEdit, QVBoxLayout
from MainWindow import loadafile
info=loadafile()
class SecondWindow(QWidget):
def __init__(self):
super(SecondWindow, self).__init__()
self.text = QTextEdit(self)
self.init_ui()
def init_ui(self):
v_layout = QVBoxLayout()
v_layout.addWidget(self.text)
self.setLayout(v_layout)
self.setText(info)
self.setWindowTitle('Opened Text')
self.show()
app = QApplication(sys.argv)
shower = SecondWindow()
sys.exit(app.exec_())
I think the loadafile does return my file_text variable but I don't know how to open the new window from there. I think I need to use a destructor for main window and then show the new window but I'm not sure of how to do this (This is the first time I try OOP)
A program is not a set of files, especially in OOP a program is the interactions of objects. And the objects interact if they have the same scope, so both windows must be created in one place so that the information from one pass to the other.
On the other hand in Qt there is a fundamental concept that is the signals, this functionality allows to notify the change of a state to another object without a lot of dependency, so in this case I will create a signal that transmits the text to the other object.
NewWindow.py
from PyQt5 import QtWidgets
class SecondWindow(QtWidgets.QWidget):
def __init__(self):
super(SecondWindow, self).__init__()
self.text = QtWidgets.QTextEdit(self)
self.init_ui()
def init_ui(self):
v_layout = QtWidgets.QVBoxLayout(self)
v_layout.addWidget(self.text)
self.setWindowTitle('Opened Text')
self.show()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
shower = SecondWindow()
sys.exit(app.exec_())
MainWindow.py
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from NewWindow import SecondWindow
class Window(QtWidgets.QWidget):
textChanged = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.img =QtWidgets.QLabel()
self.relleno=QtWidgets.QLabel()
self.btn_load = QtWidgets.QPushButton('Load')
self.width = 400
self.height = 150
self.init_ui()
def init_ui(self):
self.img.setPixmap(QtGui.QPixmap("someimage.png"))
h_final = QtWidgets.QHBoxLayout(self)
h_final.addWidget(self.img)
h_final.addWidget(self.btn_load)
self.btn_load.clicked.connect(self.loadafile)
self.setWindowTitle('This is main window')
self.setGeometry(600,150,self.width,self.height)
self.show()
#QtCore.pyqtSlot()
def loadafile(self):
filename, _ = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File', os.getenv('HOME'))
with open(filename, 'r') as f:
file_text = f.read()
self.textChanged.emit(file_text)
def main():
app = QtWidgets.QApplication(sys.argv)
main = Window()
s = SecondWindow()
main.textChanged.connect(s.text.append)
sys.exit(app.exec_())
if __name__ == '__main__':
main()

PyQt5: Find which QPushButton is clicked

I have multiple buttons & want to know which button is clicked. I have found out the error and know that the sender() function has to work with QWidget rather than the class object, but I can't figure out the solution.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self.sender() # Error Ui_MainWindow has no attribute sender
print(sender_button)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
With Qt 4.8 and python2.7, i use partial to pass multi arguments to signal.
from functools import partial
...
def initGui(self):
...
self.btn1.clicked.connect(partial(self.btnListener, "btn1"))
self.btn2.clicked.connect(partial(self.btnListener, "btn2"))
...
def btnListener(self, button_name):
print('button_name {}'.format(button_name))
...
With this method, you can know which button is clicked.
I hope that help you to find the same in QT5.
You need to inherit Ui_MainWindow from MainWindow for this to work.
class Ui_MainWindow(MainWindow):
...
below code worked well.
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class DlgMain(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Yeh lay")
self.resize(400,400)
self.btn1 = QPushButton("btn1", self)
self.btn1.move(200,200)
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QPushButton("btn2", self)
self.btn2.move(200,230)
self.btn2.clicked.connect(self.btnListener)
def btnListener(self):
rbt = self.sender()
print(rbt.text())
if __name__ =='__main__':
app = QApplication(sys.argv)
dlgMain = DlgMain()
dlgMain.show()
sys.exit(app.exec_())
here's a way:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(Ui_MainWindow, self).__init__()
self.setupUI()
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self..sender()
print(sender_button.text())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Done this based on #MrLeeh suggestion & it has worked
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.MainWindow = MainWindow
"""Widget code here"""
self.btn1 = QtWidgets.QPushButton(self.widget)
"""Button properties here"""
self.btn1.setObjectName("btn1")
self.btn1.clicked.connect(self.btnListener)
self.btn2 = QtWidgets.QPushButton(self.widget)
self.btn2.setObjectName("btn2")
self.btn2.clicked.connect(self.btnListener)
"""..... more buttons"""
def btnListener(self):
sender_button = self.MainWindow.sender()
print(sender_button.text())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Categories