Emit() a Signal to a different class isn't working - python

I am currently working on a GUI with PyQt5 (I'm a noob when it comes to python and Qt) and I need to emit a Signal from one class to another.
I read about this and googled around and also found a lot of helpful stuff but it still doesn't work for me.
This is my code-dummy:
Class Nr.1:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import Class2
class Class1(QWidget):
eventButtonPressed = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.Class1Btn = QPushButton('Button')
self.Class1Edit = QLineEdit(self)
self.Class1Btn.clicked.connect(self.clicked)
# Layout stuff to mimic my real program
self.Class1Grid = QGridLayout(self)
self.Class1Grid.addWidget(self.Class1Btn)
self.Class1Grid.addWidget(self.Class1Edit)
self.groupBoxLayout1 = QGroupBox(self)
self.groupBoxLayout1.setLayout(self.Class1Grid)
def clicked(self):
self.eventButtonPressed.emit(self.Class1Edit.text())
Class Nr.2:
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import Class1
class Class2(QWidget):
def __init__(self):
super().__init__()
self.Class1OBJ = Class1.Class1(self)
self.Class1OBJ.eventButtonPressed.connect(self.StuffWhenSignalIsEmitted)
# Layout stuff to mimic my real program
self.Class2Edit = QLineEdit(self)
self.Class2Grid = QGridLayout(self)
self.Class2Grid.addWidget(self.Class2Edit)
self.groupBoxLayout2 = QGroupBox(self)
self.groupBoxLayout2.setLayout(self.Class2Grid)
def StuffWhenSignalIsEmitted(self, text):
print('Text from Class 2 Widget: {}'.format(self.Class2Edit))
print('Text from Class 1 Widget: {}'.format(text))
My Main Window:
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import Class1
import Class2
class MainWindow(QWidget, QApplication):
def __init__(self):
super().__init__()
self.Class1OBJ = Class1.Class1()
self.Class2OBJ = Class2.Class2()
self.WinLayout = QVBoxLayout(self)
self.WinLayout.addWidget(self.Class1OBJ.groupBoxLayout1)
self.WinLayout.addWidget(self.Class2OBJ.groupBoxLayout2)
self.setGeometry(1100, 300, 300, 300)
self.setWindowTitle("GUI")
self.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I want the program to print the statements from the StuffWhenSignalIsEmitted method when I press the button (if it is possible). So I want to print what is in the LineEdit from Class one, as well as what is in the LineEdit from Class2.

It seems that you think that if a variable has the same name in different classes it is the same variable, because they are not, they are different objects. The self.Class1OBJ in Class2 is different from the self.Class1OBJ in MainWindow.
So the solution is just to create a single self.Class1OBJ:
Class2.py
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class Class2(QWidget):
def __init__(self):
super().__init__()
# Layout stuff to mimic my real program
self.Class2Edit = QLineEdit(self)
self.Class2Grid = QGridLayout(self)
self.Class2Grid.addWidget(self.Class2Edit)
self.groupBoxLayout2 = QGroupBox(self)
self.groupBoxLayout2.setLayout(self.Class2Grid)
def StuffWhenSignalIsEmitted(self, text):
print('Text from Class 2 Widget: {}'.format(self.Class2Edit))
print('Text from Class 1 Widget: {}'.format(text))
main.py
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import Class1
import Class2
class MainWindow(QWidget, QApplication):
def __init__(self):
super().__init__()
self.Class1OBJ = Class1.Class1()
self.Class2OBJ = Class2.Class2()
# add the following line
self.Class1OBJ.eventButtonPressed.connect(self.Class2OBJ.StuffWhenSignalIsEmitted)
self.WinLayout = QVBoxLayout(self)
self.WinLayout.addWidget(self.Class1OBJ.groupBoxLayout1)
self.WinLayout.addWidget(self.Class2OBJ.groupBoxLayout2)
self.setGeometry(1100, 300, 300, 300)
self.setWindowTitle("GUI")
self.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Try it:
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class ClassTwo(QWidget):
def __init__(self):
super().__init__()
self.classOneOBJ = ClassOne(self) # + self
self.classOneOBJ.eventButtonPressed.connect(self.StuffWhenSignalIsEmitted)
layout = QGridLayout(self)
layout.addWidget(self.classOneOBJ)
def StuffWhenSignalIsEmitted(self, text):
print('it worked ->{}'.format(text))
# and do stuff with instance variables of an existing object
class ClassOne(QWidget):
eventButtonPressed = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.lineEdit = QLineEdit()
self.Btn = QPushButton('Button')
self.Btn.clicked.connect(self.clicked)
layout = QGridLayout(self)
layout.addWidget(self.lineEdit)
layout.addWidget(self.Btn)
def clicked(self):
self.eventButtonPressed.emit(self.lineEdit.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
main = ClassTwo()
main.show()
sys.exit(app.exec_())

Related

PyQt5 custom widget split backghround?

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()

PyQt5 POO Call instance of Class in another class

I would like when I click on a buttom from a toolbar created with PyQt get the selected items in a QListWidget created in other class (LisWorkDirectory class).
In the ToolBar.py in the compilation_ function, I would like to get all selected items. I want to get the instance of the ListWorkDirectory class to get the QListWidget that I created the first time I have launched my app.
If I instanciate ListWorkDirectory, I get a new instance, and the selectedItems return a empty list, it is a normal behaviour.
Maybe my architecture is not correct, so if you have any suggestion or remarks don't' hesitate to learn me.
Below my code so that you understand my request :
main.py
from MainWindow import MainWindow
from MenuBar import MenuBar
from ToolBar import ToolBar
from PyQt5.QtWidgets import QApplication
import sys
app = QApplication(sys.argv)
#Window
windowApp = MainWindow("pyCompile")
#MenuBar
menuBar = MenuBar()
#ToolBar
toolBar = ToolBar()
windowApp.setMenuBar(menuBar)
windowApp.addToolBar(toolBar)
windowApp.show()
sys.exit(app.exec_())
MainWindow.py
from PyQt5.QtWidgets import QMainWindow, QWidget, QLineEdit, QPushButton, QListWidget,QListWidgetItem,QPlainTextEdit, QFileDialog, QStatusBar ,QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
from ListWorkDirectory import ListWorkDirectory
from Logger import MyDialog
import logging
import os
class MainWindow(QMainWindow):
def __init__(self, windowTitle):
super().__init__()
#Logger
self.logger = MyDialog()
logging.info("pyCompile version 0.1")
self.setGeometry(150,250,600,350)
self.setWindowTitle(windowTitle)
self.workDirectoryField = QLineEdit()
self.workDirectoryField.setPlaceholderText("Select your work directory ...")
self.workDirectoryField.setText("F:/WORKSPACE/Projects")
self.workDirectoryButton = QPushButton()
self.workDirectoryButton.setIcon(QIcon(":folder.svg"))
self.workDirectoryButton.clicked.connect(self.launchDialog)
self.hBoxLayout = QHBoxLayout()
self.hBoxLayout.addWidget(self.workDirectoryField)
self.hBoxLayout.addWidget(self.workDirectoryButton)
#List folder in work directory
self.myListFolder = ListWorkDirectory()
print(self.myListFolder)
self.workDirectoryField.textChanged[str].connect(self.myListFolder.update)
self.hBoxLayoutLogger = QHBoxLayout()
self.hBoxLayoutLogger.addWidget(self.myListFolder)
self.hBoxLayoutLogger2 = QHBoxLayout()
self.hBoxLayoutLogger2.addWidget(self.logger)
self.centralWidget = QWidget(self)
self.setCentralWidget(self.centralWidget)
self.vBoxLayout = QVBoxLayout(self.centralWidget)
self.vBoxLayout.addLayout(self.hBoxLayout)
self.vBoxLayout.addLayout(self.hBoxLayoutLogger)
self.vBoxLayout.addLayout(self.hBoxLayoutLogger2)
#Status Bar
self.statusBar = QStatusBar()
self.statusBar.showMessage("Welcome in pyCompile", 5000)
self.setStatusBar(self.statusBar)
def launchDialog(self):
workDirectory = QFileDialog.getExistingDirectory(self, caption="Select work directory")
print(workDirectory)
self.workDirectoryField.setText(workDirectory)
MenuBar.py
from PyQt5.QtWidgets import QMenuBar
class MenuBar(QMenuBar):
def __init__(self):
super().__init__()
self.fileMenu = "&File"
self.editMenu = "&Edit"
self.helpMenu = "&Help"
self.initUI()
def initUI(self):
self.addMenu(self.fileMenu)
self.addMenu(self.editMenu)
self.addMenu(self.helpMenu)
ToolBar.py
from PyQt5.QtWidgets import QMainWindow, QToolBar, QAction
from PyQt5.QtGui import QIcon
import qrc_resources
from ListWorkDirectory import ListWorkDirectory
class ToolBar(QToolBar, ListWorkDirectory):
def __init__(self):
super().__init__()
self._createActions()
self.initUI()
def initUI(self):
self.setMovable(False)
self.addAction(self.compileAction)
self.addAction(self.settingsAction)
self.addAction(self.quitAction)
def _createActions(self):
self.compileAction = QAction(self)
self.compileAction.setStatusTip("Launch compilation")
self.compileAction.setText("&Compile")
self.compileAction.setIcon(QIcon(":compile.svg"))
self.compileAction.triggered.connect(self.compilation_)
self.settingsAction = QAction(self)
self.settingsAction.setText("&Settings")
self.settingsAction.setIcon(QIcon(":settings.svg"))
self.quitAction = QAction(self)
self.quitAction.setText("&Quit")
self.quitAction.setIcon(QIcon(":quit.svg"))
def compilation_(self):
"""
Get the instance of ListWorkDirectory to get selected items and launch the
compilation
"""
ListWorkDirectory.py
from PyQt5.QtWidgets import QListWidget,QListWidgetItem
from PyQt5.QtCore import Qt, QDir
import os
class ListWorkDirectory(QListWidget):
def __init__(self):
super().__init__()
self.clear()
def update(self, workDirectoryField):
isPathCorrect = self.checkPath(workDirectoryField)
if(isPathCorrect):
listOfDirectory = self.getFolderList(workDirectoryField, os.listdir(workDirectoryField))
for folder in listOfDirectory:
self.item = QListWidgetItem(folder)
self.item.setCheckState(Qt.Unchecked)
self.addItem(self.item)
else:
self.clear()
def checkPath(self, path):
QPath = QDir(path)
isQPathExist = QPath.exists()
isPathEmpty = self.isPathEmpty(path)
if(isQPathExist and not isPathEmpty):
return True
else:
return False
def getFolderList(self, path, listOfFiles):
listOfFolders=[]
for file_ in listOfFiles:
if(os.path.isdir(os.path.join(path, file_))):
listOfFolders.append(file_)
else:
pass
return listOfFolders
def isPathEmpty(self, path):
if(path != ""):
return False
else:
return True
Thank you for your help.
When you add widget to window (or to other widget) then this window (or widget) is its parent and you can use self.parent() to access element in window (or widget). When widgets are nested then you may even use self.parent().parent()
def compilation_(self):
"""
Get the instance of ListWorkDirectory to get selected items and launch the
compilation
"""
print(self.parent().myListFolder)
EDIT:
Class ListWorkDirectory has function item(number) to get item from list - but you overwrite it with line self.item = QListWidgetItem(folder). If you remove self. and use
item = QListWidgetItem(folder)
item.setCheckState(Qt.Unchecked)
self.addItem(item)
then this will show only checked items
def compilation_(self):
"""
Get the instance of ListWorkDirectory to get selected items and launch the
compilation
"""
lst = self.parent().myListFolder
for x in range(lst.count()):
item = lst.item(x)
#print(item.checkState(), item.text())
if item.checkState() :
print(item.text())
Full working code - everyone can simply copy all to one file and run it.
from PyQt5.QtWidgets import QMainWindow, QWidget, QLineEdit, QPushButton, QListWidget,QListWidgetItem,QPlainTextEdit, QFileDialog, QStatusBar ,QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import logging
import os
# ---
from PyQt5.QtWidgets import QMenuBar
class MenuBar(QMenuBar):
def __init__(self):
super().__init__()
self.fileMenu = "&File"
self.editMenu = "&Edit"
self.helpMenu = "&Help"
self.initUI()
def initUI(self):
self.addMenu(self.fileMenu)
self.addMenu(self.editMenu)
self.addMenu(self.helpMenu)
# ---
from PyQt5.QtWidgets import QMainWindow, QToolBar, QAction
from PyQt5.QtGui import QIcon
class ToolBar(QToolBar):
def __init__(self):
super().__init__()
self._createActions()
self.initUI()
def initUI(self):
self.setMovable(False)
self.addAction(self.compileAction)
self.addAction(self.settingsAction)
self.addAction(self.quitAction)
def _createActions(self):
self.compileAction = QAction(self)
self.compileAction.setStatusTip("Launch compilation")
self.compileAction.setText("&Compile")
self.compileAction.setIcon(QIcon(":compile.svg"))
self.compileAction.triggered.connect(self.compilation_)
self.settingsAction = QAction(self)
self.settingsAction.setText("&Settings")
self.settingsAction.setIcon(QIcon(":settings.svg"))
self.quitAction = QAction(self)
self.quitAction.setText("&Quit")
self.quitAction.setIcon(QIcon(":quit.svg"))
def compilation_(self):
"""
Get the instance of ListWorkDirectory to get selected items and launch the
compilation
"""
lst = self.parent().myListFolder
for x in range(lst.count()):
item = lst.item(x)
#print(item.checkState(), item.text())
if item.checkState() :
print(item.text())
# ---
from PyQt5.QtWidgets import QListWidget, QListWidgetItem
from PyQt5.QtCore import Qt, QDir
import os
class ListWorkDirectory(QListWidget):
def __init__(self):
super().__init__()
self.clear()
def update(self, workDirectoryField):
isPathCorrect = self.checkPath(workDirectoryField)
if(isPathCorrect):
listOfDirectory = self.getFolderList(workDirectoryField, os.listdir(workDirectoryField))
for folder in listOfDirectory:
item = QListWidgetItem(folder)
item.setCheckState(Qt.Unchecked)
self.addItem(item)
else:
self.clear()
def checkPath(self, path):
QPath = QDir(path)
isQPathExist = QPath.exists()
isPathEmpty = self.isPathEmpty(path)
if(isQPathExist and not isPathEmpty):
return True
else:
return False
def getFolderList(self, path, listOfFiles):
listOfFolders=[]
for file_ in listOfFiles:
if(os.path.isdir(os.path.join(path, file_))):
listOfFolders.append(file_)
else:
pass
return listOfFolders
def isPathEmpty(self, path):
if(path != ""):
return False
else:
return True
class MainWindow(QMainWindow):
def __init__(self, windowTitle):
super().__init__()
#Logger
#self.logger = MyDialog()
logging.info("pyCompile version 0.1")
self.setGeometry(150,250,600,350)
self.setWindowTitle(windowTitle)
self.workDirectoryField = QLineEdit()
self.workDirectoryField.setPlaceholderText("Select your work directory ...")
self.workDirectoryField.setText("F:/WORKSPACE/Projects")
self.workDirectoryButton = QPushButton()
self.workDirectoryButton.setIcon(QIcon(":folder.svg"))
self.workDirectoryButton.clicked.connect(self.launchDialog)
self.hBoxLayout = QHBoxLayout()
self.hBoxLayout.addWidget(self.workDirectoryField)
self.hBoxLayout.addWidget(self.workDirectoryButton)
#List folder in work directory
self.myListFolder = ListWorkDirectory()
print(self.myListFolder)
self.workDirectoryField.textChanged[str].connect(self.myListFolder.update)
self.hBoxLayoutLogger = QHBoxLayout()
self.hBoxLayoutLogger.addWidget(self.myListFolder)
self.hBoxLayoutLogger2 = QHBoxLayout()
#self.hBoxLayoutLogger2.addWidget(self.logger)
self.centralWidget = QWidget(self)
self.setCentralWidget(self.centralWidget)
self.vBoxLayout = QVBoxLayout(self.centralWidget)
self.vBoxLayout.addLayout(self.hBoxLayout)
self.vBoxLayout.addLayout(self.hBoxLayoutLogger)
self.vBoxLayout.addLayout(self.hBoxLayoutLogger2)
#Status Bar
self.statusBar = QStatusBar()
self.statusBar.showMessage("Welcome in pyCompile", 5000)
self.setStatusBar(self.statusBar)
def launchDialog(self):
workDirectory = QFileDialog.getExistingDirectory(self, caption="Select work directory")
print(workDirectory)
self.workDirectoryField.setText(workDirectory)
# ---
from PyQt5.QtWidgets import QApplication
import sys
app = QApplication(sys.argv)
#Window
windowApp = MainWindow("pyCompile")
#MenuBar
menuBar = MenuBar()
#ToolBar
toolBar = ToolBar()
windowApp.setMenuBar(menuBar)
windowApp.addToolBar(toolBar)
windowApp.show()
sys.exit(app.exec_())

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 create a two or more Color Custom QPushButton in PyQt5?

How to Create a Custom Button with two or More color text and as well as in With double or single underline(in a particular letter)? I tried my level best. But the Blank button (no text) only appears.
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class MyButton(QPushButton):
def __init__ (self, mytext,parent=None):
super(MyButton,self).__init__()
self.mytext = mytext
def paintEvent(self, event):
document = QTextDocument()
document.setDocumentMargin(0)
document.setHtml(mytext)
mypixmap=QPixmap(document.size().tosize())
mypixmap.fill(Qt.transparent)
painter = QPainter(mypixmap)
document.drawContents(painter)
painter.end()
class CustomButton(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Grid layout Example")
self.setGeometry(100,100,400,400)
self.widget()
self.show()
def widget(self):
self.btn_sample = MyButton(QIcon("<h2><i>My sample</i> <font color=red>Button!</font></h2>"))
self.btn_sample.resize(20,20)
self.layout = QVBoxLayout()
self.layout.addWidget(self.btn_sample)
self.setLayout(self.layout)
def main():
app = QApplication(sys.argv)
mainwindow = CustomButton()
mainwindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class MyButton(QPushButton):
def __init__(self, Text, parent = None):
super(MyButton, self).__init__()
mydocument = QTextDocument()
mydocument.setDocumentMargin(0)
mydocument.setHtml(Text)
mypixmap = QPixmap(mydocument.size().toSize())
mypixmap.fill(Qt.transparent)
mypainter = QPainter(mypixmap)
mydocument.drawContents(mypainter)
mypainter.end()
myicon = QIcon(mypixmap)
self.setIcon(myicon)
self.setIconSize(mypixmap.size())
class mainwindow(QWidget):
def __init__(self , parent = None):
super(mainwindow, self).__init__()
self.setupgui()
def setupgui(self):
self.resize(800,600)
self.setWindowTitle('Custom Button With two Color Text')
newLayout = QHBoxLayout()
self.dashboard = MyButton("<h2><i>Dash Board</i> <font color=red>Qt!</font></h2>",self)
self.transcation = MyButton('<font color="red"><u>T</u></font><font color="black">ranscation</font>',self)
newLayout.addWidget(self.dashboard)
newLayout.addWidget(self.transcation)
self.setLayout(newLayout)
self.show()
def main():
app = QApplication(sys.argv)
ex = mainwindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

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()

Categories