Continuing from previous article, I will play with PyQt5.
This time I will open a sub window
test.py
#!/usr/bin/env python3
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
#Creating a button
makeWindowButton = QPushButton("&make window")
#Click event(makeWindow())Add
makeWindowButton.clicked.connect(self.makeWindow)
#Creating a layout
layout = QHBoxLayout()
#Adding buttons to the layout
layout.addWidget(makeWindowButton)
#Add layout to window
self.setLayout(layout)
def makeWindow(self):
#Creating a subwindow
subWindow = SubWindow()
#Show subwindow
subWindow.show()
class SubWindow(QWidget):
def __init__(self, parent=None):
#Is this the entity of the subwindow? Target. dialog
self.w = QDialog(parent)
label = QLabel()
label.setText('Sub Window')
layout = QHBoxLayout()
layout.addWidget(label)
self.w.setLayout(layout)
def show(self):
self.w.exec_()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
Instantiate a subwindow class in the parent window and show () to create a subwindow.
self.w = QDialog(parent)
Here and there, it's important.
Without doing this, in the subwindow class
self.setLayout(layout)
No, it will cause an error.
There are probably situations where you want to output a subwindow and enter parameters. I'll try.
test.py
#!/usr/bin/env python3
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class MainWindow(QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
makeWindowButton = QPushButton("&make window")
makeWindowButton.clicked.connect(self.makeWindow)
self.label = QLabel()
self.label.setText("default:")
layout = QHBoxLayout()
layout.addWidget(makeWindowButton)
layout.addWidget(self.label)
self.setLayout(layout)
def makeWindow(self):
subWindow = SubWindow(self)
subWindow.show()
#Run from subwindow
def setParam(self, param):
self.label.setText(param)
class SubWindow:
def __init__(self, parent=None):
self.w = QDialog(parent)
self.parent = parent
label = QLabel()
label.setText('Sub Window')
self.edit = QLineEdit()
button = QPushButton('Send')
button.clicked.connect(self.setParamOriginal)
layout = QVBoxLayout()
layout.addWidget(label)
layout.addWidget(self.edit)
layout.addWidget(button)
self.w.setLayout(layout)
#I'm passing a value to the parent window here
def setParamOriginal(self):
self.parent.setParam(self.edit.text())
def show(self):
self.w.exec_()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
I'm putting values from the subwindow to the label of the parent window. It's been 4 days for the first time in Python, and I haven't studied much about the Python language, so I was a little stuck because I didn't know how to handle self. I'm sorry the code is dirty.
PyQt It's really amazing, it's amazing ... I thought it was very convenient. I'm trying to read the reference, but I can't read it because I'm not fluent in English and I'm not used to Python yet. I have to become a little more Python. .. ..
Please excuse me because the code is dirty ...
Recommended Posts