Opening a second window PyQt - python

I have seen other examples of this but they use
class secondWindow():
def __init__(self):
super().__init__()
#some code
class Window():
def __init__(self):
super().__init__()
# some code
def main():
app = QtGui.QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
however my code looks like this
class secondWindow():
def __init__(self):
some code
class overviewWindow():
def __init__(self):
# Database setup
conn = None
try:
conn = sqlite3.connect("exercises.db")
print("[*] Connected successfully to the database")
except:
print("[*] Error connecting to the databse")
# Window setup
self.app = QApplication(sys.argv)
self.w = QWidget()
self.w.resize(winWidth, winHeight)
self.w.move(200, 20)
self.w.setWindowTitle("Test")
self.w.setStyleSheet("background-color: #F4F0BB")
self.w.show()
self.showUI()
sys.exit(self.app.exec_())
def showUI(self):
# Title
self.title = QLabel()
self.title.setParent(self.w)
self.title.setText("Overblik")
self.title.resize(winWidth, 100)
self.title.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.title.setStyleSheet("font-size: 45pt")
self.title.show()
self.frame = QFrame()
self.frame.resize(winWidth, 900)
self.frame.move(0, 100)
self.frame.setParent(self.w)
self.frame.show()
# Buttons
xBuffer = 130
yBuffer = 130
sum = 0
for i in range(0, 6):
for j in range(0, 4):
sum += 1
self.button2 = Button(self.frame, 75+(j*xBuffer), 75+(i*yBuffer), str(sum))
if __name__ == "__main__":
overviewWindow()
My problem is i want to open a second window with the same window style (same background and size) as the first but with different content i.e. different labels and buttons
I have tried modifying my code so it looks like the other examples but i cant figure out how to rewrite it without rewriting my entire file.

Try having all your styles and customization in a stylesheet that way you can apply the style to all classes you need to without individually changing elements here is an example:
from PySide2 import QtCore
from PySide2.QtWidgets import *
from PySide2.QtGui import *
class Widget2(QWidget):
def __init__(self, parent=None):
super(Widget2, self).__init__(parent)
self.gui()
def gui(self):
self.w1 = self
self.w1.setAutoFillBackground(True)
self.w1.setWindowTitle("")
self.w1.resize(500, 450)
self.w1.setCursor(Qt.ArrowCursor)
self.w1.setToolTip("")
self.check1 = QCheckBox("Check Box", self.w1)
self.check1.setChecked(0)
self.check1.move(180, 220)
self.check1.resize(90, 22)
self.check1.setCursor(Qt.ArrowCursor)
self.check1.setToolTip("")
self.button2 = QToolButton(self.w1)
self.button2.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
self.button2.setText("Button")
self.button2.move(40, 110)
self.button2.resize(90, 22)
self.button2.setCursor(Qt.ArrowCursor)
self.button2.setToolTip("")
return self.w1
class Widget1(QWidget):
def __init__(self, parent=None):
super(Widget1, self).__init__(parent)
self.gui()
def gui(self):
self.w1 = self
self.w1.setAutoFillBackground(True)
self.w1.setWindowTitle("")
self.w1.resize(500, 450)
self.w1.setCursor(Qt.ArrowCursor)
self.w1.setToolTip("")
self.button1 = QToolButton(self.w1)
self.button1.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
self.button1.setText("Button")
self.button1.move(40, 110)
self.button1.resize(90, 22)
self.button1.setCursor(Qt.ArrowCursor)
self.button1.setToolTip("")
self.button1.clicked.connect(self.event)
return self.w1
def event(self):
b = Widget2()
b.setStyleSheet("""
QToolButton
{
color: yellow;
background-color: red;
}""")
b.show()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
a = Widget1()
a.setStyleSheet("""
QToolButton
{
color: yellow;
background-color: red;
}""")
a.show()
sys.exit(app.exec_())
As you can see I have one stylesheet that is applied to both of my QWidgets, ideally you would apply the stylesheet to QApplication but I could not reproduce an example of it working.
If your window opens and closes straight away try having this in the event function instead:
def event(self):
if self.w is None:
self.w = SecondWindow()
self.w.show()
Or have self.w = SecondWindow() in the main function and then in the event have the self.w.show().
So like this:
class Second(QtGui.QMainWindow):
some code
class First(QtGui.QMainWindow):
def __init__(self, parent=None):
some code
self.w = SecondWindow()
def on_pushButton_clicked(self):
self.w.show()

Related

PyQt5 retrieving ID and values from multiple Buttons created in an open loop [duplicate]

This question already has answers here:
How to determine which widget emitted the signal
(1 answer)
How do I assert the identity of a PyQt5 signal?
(2 answers)
Closed 2 years ago.
I'm trying to create buttons (QPushButtons) based on an entry (QLineEdit). The idea is that I want the user to be able to create as many buttons as wanted, simply by adding new text in the entry box and by then pressing "Add Label" (see picture below).
While I'm able to do this, I can't for now retrieve the label value of each of these buttons, since the procedure I use erases all the previous values (I can only retrieve the last value entered). I'd like to be able to print each specific Label Value when clicking each button.
My code is below:
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit
import sys
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(100, 100, 1500, 1500)
self.setWindowTitle("My Program")
self.labelButtons = [] # List of all the buttons displaying labels
self.eraseButtons = [] # List of all the buttons displaying "X"
self.Yposition = 50
self.initUI()
def initUI(self):
self.labelEntry = QLineEdit(self)
self.labelEntry.move(50, self.Yposition)
self.labelEntry.resize(300, 40)
self.addLabelButton = QPushButton(self)
self.addLabelButton.setText("Add Label")
self.addLabelButton.move(400, self.Yposition)
self.addLabelButton.resize(300, 40)
self.addLabelButton.clicked.connect(self.addNewLabel)
def addNewLabel(self):
self.Yposition += 50
self.newLabelName = self.labelEntry.text()
self.labelButtons.append(self.createButtonLabel(self.newLabelName))
self.eraseButtons.append(self.eraseButtonLabel())
self.updatelabels()
def createButtonLabel(self, labelname):
self.button = QPushButton(self)
self.button.setText(str(labelname))
self.button.resize(300, 40)
self.button.move(50, self.Yposition)
self.button.clicked.connect(self.printbutton)
return self.button
def eraseButtonLabel(self):
self.buttonErase = QPushButton(self)
self.buttonErase.setText("X")
self.buttonErase.resize(40, 40)
self.buttonErase.move(360, self.Yposition)
self.buttonErase.clicked.connect(self.printbutton)
return self.buttonErase
def updatelabels(self):
for button in self.labelButtons:
button.show()
for button in self.eraseButtons:
button.show()
def printbutton(self):
print(self.button.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
<!-- end snippet -->
Using Google pyqt clicked event I found you have to use
def printbutton(self):
widget = self.sender()
print(widget.text())
ZetCode: Events and signals in PyQt5
EDIT:
As for erease button - you should get button from createButtonLabel and send it to eraseButtonLabel
labelbutton = self.createButtonLabel(self.newLabelName)
erasebutton = self.eraseButtonLabel(labelbutton)
and you can use lambda to assing function with argument
def eraseButtonLabel(self, labelbutton):
# ... code ...
self.buttonErase.clicked.connect(lambda: self.erasebutton(labelbutton))
and function should get this argument
def erasebutton(self, button):
widget = self.sender()
print('clicked:', widget.text())
print(' erase:', button.text())
Or you can assing button to own variable in buttonErase
def eraseButtonLabel(self, labelbutton):
# ... code ...
self.buttonErase.assigned_button = labelbutton
self.buttonErase.clicked.connect(self.erasebutton)
and use it in function
def erasebutton(self):
widget = self.sender()
print('clicked:', widget.text())
print(' erase:', widget.assigned_button.text())
Full code which uses both methods at the same time but you need only one method.
from PyQt5.QtWidgets import *
import sys
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(100, 100, 1500, 1500)
self.setWindowTitle("My Program")
self.labelButtons = [] # List of all the buttons displaying labels
self.eraseButtons = [] # List of all the buttons displaying "X"
self.Yposition = 50
self.initUI()
def initUI(self):
self.labelEntry = QLineEdit(self)
self.labelEntry.move(50, self.Yposition)
self.labelEntry.resize(300, 40)
self.addLabelButton = QPushButton(self)
self.addLabelButton.setText("Add Label")
self.addLabelButton.move(400, self.Yposition)
self.addLabelButton.resize(300, 40)
self.addLabelButton.clicked.connect(self.addNewLabel)
def addNewLabel(self):
self.Yposition += 50
self.newLabelName = self.labelEntry.text()
labelbutton = self.createButtonLabel(self.newLabelName)
erasebutton = self.eraseButtonLabel(labelbutton)
self.labelButtons.append(labelbutton)
self.eraseButtons.append(erasebutton)
self.updatelabels()
def createButtonLabel(self, labelname):
self.button = QPushButton(self)
self.button.setText(str(labelname))
self.button.resize(300, 40)
self.button.move(50, self.Yposition)
self.button.clicked.connect(self.printbutton)
return self.button
def eraseButtonLabel(self, labelbutton):
self.buttonErase = QPushButton(self)
self.buttonErase.setText("X")
self.buttonErase.resize(40, 40)
self.buttonErase.move(360, self.Yposition)
self.buttonErase.assigned_button = labelbutton
self.buttonErase.clicked.connect(lambda: self.erasebutton(labelbutton))
#self.buttonErase.clicked.connect(self.erasebutton)
return self.buttonErase
def updatelabels(self):
for button in self.labelButtons:
button.show()
for button in self.eraseButtons:
button.show()
def printbutton(self):
print('clicked:', self.sender().text())
def erasebutton(self, button):
widget = self.sender()
print('clicked:', widget.text())
print(' erase:', button.text())
print(' erase:', widget.assigned_button.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
EDIT:
Other method is to create own widget which has both buttons labelbutton and erasebutton and then erasebutton has direct access only to own labelbutton.
BTW: and for similar reason I would keep buttons as pairs
self.buttons.append([labelbutton, erasebutton])
instead of separted lists
self.labelButtons.append(labelbutton)
self.eraseButtons.append(erasebutton)
Example in which I create own widget.
from PyQt5.QtWidgets import *
import sys
class MyWidget(QWidget):
def __init__(self, parent, labelname, *args, **kwargs):
super().__init__(parent, *args, **kwargs)
self.resize(350, 40)
self.labelButton = QPushButton(self)
self.labelButton.setText(str(labelname))
self.labelButton.resize(300, 40)
self.labelButton.move(0, 0)
self.labelButton.clicked.connect(self.printbutton)
self.buttonErase = QPushButton(self)
self.buttonErase.setText("X")
self.buttonErase.resize(40, 40)
self.buttonErase.move(310, 0)
self.buttonErase.clicked.connect(self.erasebutton)
self.show()
def printbutton(self):
print('clicked:', self.labelButton.text())
def erasebutton(self):
print('clicked:', self.buttonErase.text())
print(' erase:', self.labelButton.text())
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setGeometry(100, 100, 1500, 1500)
self.setWindowTitle("My Program")
self.widgets = []
self.Yposition = 50
self.initUI()
def initUI(self):
self.labelEntry = QLineEdit(self)
self.labelEntry.move(50, self.Yposition)
self.labelEntry.resize(300, 40)
self.addLabelButton = QPushButton(self)
self.addLabelButton.setText("Add Label")
self.addLabelButton.move(400, self.Yposition)
self.addLabelButton.resize(300, 40)
self.addLabelButton.clicked.connect(self.addNewLabel)
def addNewLabel(self):
self.Yposition += 50
text = self.labelEntry.text()
widget = MyWidget(self, text)
widget.move(50, self.Yposition)
self.widgets.append(widget)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())

Change QLabelText as QLineEdit text changes

There is this QLineEdit with setText is set to a predefined value and there is a QDialog with QLabel in it which is supposed to show whatever is in the QLineEdit. The code below shows the situation.
import sys
import os
import datetime
from PySide2.QtWidgets import *
from PySide2 import *
now = datetime.datetime.now()
now_str = now.strftime("%H.%M.%S,%d/%m/%y")
default_text = (str("Sugar_" + now_str))
class MainWindow(QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.resize(600, 500)
btn = QPushButton("show dialog")
mw_layout = QVBoxLayout()
mw_layout.addWidget(btn)
self.setLayout(mw_layout)
btn.clicked.connect(show_d)
class dialog(QDialog):
def __init__(self):
super(dialog, self).__init__()
self.resize(400, 350)
title = QLineEdit()
title.setText(default_text)
show_title = QPushButton("Show title")
cancel = QPushButton("Cancel")
d_layout = QVBoxLayout()
d_layout.addWidget(title)
d_layout.addWidget(show_title)
d_layout.addWidget(cancel)
self.setLayout(d_layout)
t = title.text()
title_dialog = QDialog()
label = QLabel()
label.setText("The title is " + title.text())
ok = QPushButton("OK!")
t_layout = QVBoxLayout()
t_layout.addWidget(label)
t_layout.addWidget(ok)
title_dialog.setLayout(t_layout)
def show_t():
title_dialog.exec_()
title_dialog.setModal(True)
def close_t():
title_dialog.accept()
show_title.clicked.connect(show_t)
ok.clicked.connect(close_t)
cancel.clicked.connect(self.close_d)
def close_d(self):
self.reject()
def show_d():
d = dialog()
d.exec_()
d.setModal(True)
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = MainWindow()
MainWindow.show()
sys.exit(app.exec_())
But this doesn't work like I expected it to. The QLabel text just show the default text even when the text in QLineEdit is changed.
The console also shows the following error;
qt.xkb.compose: failed to create compose table.
I think there is something's obviously wrong but I can't seem to find what.
Any help appreciated.
You must update the text before a certain event, for example an instant before displaying the dialog:
class dialog(QDialog):
def __init__(self):
super(dialog, self).__init__()
self.resize(400, 350)
self.title_lineedit = QLineEdit(default_text)
show_title = QPushButton("Show title")
cancel = QPushButton("Cancel")
d_layout = QVBoxLayout(self)
d_layout.addWidget(self.title_lineedit)
d_layout.addWidget(show_title)
d_layout.addWidget(cancel)
self.title_dialog = QDialog()
self._title_label = QLabel()
ok = QPushButton("OK!")
t_layout = QVBoxLayout(self.title_dialog)
t_layout.addWidget(self._title_label)
t_layout.addWidget(ok)
show_title.clicked.connect(self.on_clicked)
ok.clicked.connect(self.title_dialog.reject)
cancel.clicked.connect(self.reject)
self.update_label()
def update_label(self):
self._title_label.setText("The title is " + self.title_lineedit.text())
def on_clicked(self):
self.update_label()
self.title_dialog.exec_()

setting background image for a widget in another class

I've been recently learning pyqt5 as my first gui framework. So far I have been experimenting with QtStackedLayout. I currently have two window screens, one created inside the UI class and another in another separate class. I have two concerns:
Everything was working until I started experimenting on adding a background image for Window 1. There is no image displayed but the code runs ok.
There is this small fraction of time in the beginning where window one will get displayed first until it gets loaded to the mainwindow, I tried passing self during instantiation of the object to sert as some kind of parent to prevent this but I think I'm not doing it right.
See below code (I have bad import statements, I will sort it out)
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Ui(QWidget):
def setupUi(self, Main, width, height):
self.stack = QStackedLayout()
self.window_1 = WindowOne(width, height)
self.window_2 = QWidget(self)
self.window_2_UI()
self.stack.addWidget(self.window_1)
self.stack.addWidget(self.window_2)
# Only one button
self.btn = QPushButton("Change window", self)
# Create the central widget of your Main Window
self.main_widget = QWidget(self)
layout = QVBoxLayout(self.main_widget)
layout.addLayout(self.stack)
layout.addWidget(self.btn)
self.setCentralWidget(self.main_widget)
self.btn.clicked.connect(self.change_window)
def change_window(self):
if self.stack.currentIndex() == 0:
self.stack.setCurrentIndex(1)
else:
self.stack.setCurrentIndex(0)
def window_2_UI(self):
label = QLabel("In Window 2", self.window_2)
class WindowOne(QWidget):
def __init__(self, width, height):
super().__init__()
self.set_bg(width, height)
self.set_label()
# self.setStyleSheet("background-image: url(:resource/images/blue_bg.jpg)")
def set_label(self):
label = QLabel("In Window 1", self)
def set_bg(self, w, h):
oImage = QImage("resource/images/blue_bg.jpg")
sImage = oImage.scaled(QSize(w, h))
palette = QPalette()
palette.setBrush(10, QBrush(sImage))
self.setPalette(palette)
class Main(QMainWindow, Ui):
def __init__(self):
super().__init__()
self.w_width = 480
self.w_height = 720
self.resize(self.w_width, self.w_height)
self.init_ui()
self.setupUi(self, self.w_width, self.w_height)
def init_ui(self):
self.center()
self.setWindowTitle('Main Window')
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
if __name__ == "__main__":
app = QApplication(sys.argv)
M = Main()
M.show()
sys.exit(app.exec())
By default only the window (which is different to a widget) will use the background color of QPalette, if you want a widget to use the background color of QPalette you must enable the autoFillBackground property.
# ...
self.set_label(width, height)
self.setAutoFillBackground(True)
# ...
Although your code is a little messy, for example you establish that certain methods receive certain parameters but you never use them. Finally I think you want the background of the image to be re-scale using the size of the window so I have override the resizeEvent() method so that scaling takes the size of the window.
Considering all the above, I have improved your code:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.m_stacked_layout = QtWidgets.QStackedLayout()
self.widget_1 = WidgetOne()
self.widget_2 = QtWidgets.QWidget()
self.widget_2_UI()
self.m_stacked_layout.addWidget(self.widget_1)
self.m_stacked_layout.addWidget(self.widget_2)
button = QtWidgets.QPushButton(
"Change window", clicked=self.change_window
)
lay = QtWidgets.QVBoxLayout(self)
lay.addLayout(self.m_stacked_layout)
lay.addWidget(button)
#QtCore.pyqtSlot()
def change_window(self):
ix = self.m_stacked_layout.currentIndex()
self.m_stacked_layout.setCurrentIndex(1 if ix == 0 else 0)
def widget_2_UI(self):
label = QtWidgets.QLabel("In Window 2", self.widget_2)
class WidgetOne(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setAutoFillBackground(True)
self.set_label()
self.m_image = QtGui.QImage("resource/images/blue_bg.jpg")
def set_label(self):
label = QtWidgets.QLabel("In Window 1", self)
def resizeEvent(self, event):
palette = self.palette()
sImage = self.m_image.scaled(event.size())
palette.setBrush(10, QtGui.QBrush(sImage))
self.setPalette(palette)
super(WidgetOne, self).resizeEvent(event)
class Main(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
widget = Widget()
self.setCentralWidget(widget)
self.resize(480, 720)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec())

How to update QWidget's content?

There is a QWidget, which is called with the button "Start" in the main widget.
It shows some text, which should be updated every minute during the work of the QWidget.
How can I make this infinity updating within this code?
class ExampleWidget(QWidget):
def __init__(self, parent=None):
super().__init__()
self.setWindowTitle('Example Widget ScrollArea')
self.initUi()
def initUi(self):
area = QScrollArea(self)
area.setWidgetResizable(True)
self.scrollAreaWidgetContents = QLabel(some_text, self)
area.setWidget(self.scrollAreaWidgetContents)
button = QPushButton("Close")
button.clicked.connect(self.goMainWindow)
layoutV = QVBoxLayout()
layoutV.addWidget(area)
layoutV.addWidget(button)
self.setLayout(layoutV)
def goMainWindow(self):
self.hide()
def sizeHint(self):
return QSize(400, 200)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
start_main_button = QPushButton('Start', self)
start_main_button.move(40, 40)
start_main_button.clicked.connect(self.start)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Test')
def start(self):
global some_text
some_text = 'some text'
self.result_widget = ExampleWidget()
self.result_widget.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
One of my tries:
def start(self):
global some_text
some_text = 'some text'
self.result_widget = ExampleWidget()
self.result_widget.show()
i = 0
while True:
i+=1
some_text+=str(i)
self.result_widget = ExampleWidget()
self.result_widget.show()
Forget the global variables because they are considered bad practice, on the other hand in a GUI you should avoid having loops that consume a lot of time, and in your case a while True blocks the GUI. In Qt if you want to do periodic tasks you must use a QTimer:
from PyQt5 import QtCore, QtWidgets
class ExampleWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__()
self.setWindowTitle('Example Widget ScrollArea')
self.initUi()
def initUi(self):
area = QtWidgets.QScrollArea()
area.setWidgetResizable(True)
self.scrollAreaWidgetContents = QtWidgets.QLabel("some_text")
area.setWidget(self.scrollAreaWidgetContents)
button = QtWidgets.QPushButton("Close")
button.clicked.connect(self.hide)
layoutV = QtWidgets.QVBoxLayout(self)
layoutV.addWidget(area)
layoutV.addWidget(button)
def update_text(self, text):
# update the text
self.scrollAreaWidgetContents.setText(text)
def sizeHint(self):
return QtCore.QSize(400, 200)
class MainWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
start_main_button = QtWidgets.QPushButton('Start', self)
start_main_button.move(40, 40)
start_main_button.clicked.connect(self.start)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Test')
self.result_widget = ExampleWidget()
self.timer = QtCore.QTimer(self, interval=60*1000)
self.timer.timeout.connect(self.on_timeout)
self.counter = 0
self.initial_text = "some_text"
def on_timeout(self):
# this method will be called every 60 * 1000 ms
self.initial_text += str(self.counter)
self.result_widget.update_text(self.initial_text)
self.counter += 1
#QtCore.pyqtSlot()
def start(self):
self.result_widget.show()
# start timer
self.timer.start()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

change label from other class in a different file

I am creating an application where I have a main window whit a label and then a docked widget that is in another file. I want to change the main windows label from a button at the docked widget. I try to import the main window file but then I can not access to the label. And I also tried to call a function in the main windows that changes the label but then the label does not change.
Here is the code:
main_window.py:
import results_window
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.define_main_windows()
self.create_dock_widgets()
def define_main_windows(self):
# Define de Main window properties
self.setMinimumSize(QSize(300, 100))
self.setWindowTitle("Python SkyLibris")
self.setWindowIcon(QtGui.QIcon("skylibris_icon.png"))
self.setStyleSheet("QMainWindow {background: 'white';}")
self.top = 50
self.left = 0
self.width = 1300
self.height = 400
self.setGeometry(self.left, self.top, self.width, self.height)
self.result = QLabel("result:")
self.setCentralWidget(self.result)
def create_dock_widgets(self):
# Create dock widgets
self.results_window = results_window.results_window()
self.resultsWindowDock = QDockWidget("Results Viewer", self)
self.resultsWindowDock.setWidget(self.results_window )
self.resultsWindowDock.setFloating(False)
self.resultsWindowDock.setVisible(True)
self.addDockWidget(Qt.LeftDockWidgetArea, self.resultsWindowDock)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
results_window.py:
import main_window
class results_window(QWidget):
def __init__(self):
super(results_window, self).__init__()
print("init")
self.label = QLabel()
self.value = QLineEdit()
self.bt = QPushButton("Click")
self.bt.clicked.connect(self.clickMethod)
self.main_layout = QVBoxLayout()
self.main_layout.addWidget(self.label)
self.main_layout.addWidget(self.value)
self.main_layout.addWidget(self.bt)
self.setLayout(self.main_layout)
def clickMethod(self):
print(self.value.text())
text = self.value.text()
main_window.result.setText(text)
You are using the wrong tools, for example your code has a circular import that causes your application to close since it is equivalent to a while True.
In Qt, signals and slots are used to share data asynchronously, as well as contributing to the fact that there is no coupling between classes. In your case, Results_Window must have a signal that transmits that information to the MainWindow, this signal must be emit within clickMethod.
results_window.py
from PyQt5 import QtCore, QtWidgets
class Results_Window(QtWidgets.QWidget):
resultChanged = QtCore.pyqtSignal(str)
def __init__(self):
super(Results_Window, self).__init__()
print("init")
self.label = QtWidgets.QLabel()
self.value = QtWidgets.QLineEdit()
self.bt = QtWidgets.QPushButton("Click")
self.bt.clicked.connect(self.clickMethod)
main_layout = QtWidgets.QVBoxLayout(self)
main_layout.addWidget(self.label)
main_layout.addWidget(self.value)
main_layout.addWidget(self.bt)
#QtCore.pyqtSlot()
def clickMethod(self):
text = self.value.text()
self.resultChanged.emit(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
w = Results_Window()
w.show()
sys.exit(app.exec_())
main_window.py
from PyQt5 import QtCore, QtGui, QtWidgets
import results_window
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.define_main_windows()
self.create_dock_widgets()
def define_main_windows(self):
self.setMinimumSize(QtCore.QSize(300, 100))
self.setWindowTitle("Python SkyLibris")
self.setWindowIcon(QtGui.QIcon("skylibris_icon.png"))
self.setStyleSheet("QMainWindow {background: 'white';}")
top, left, width, height = 50, 0, 1300, 400
self.setGeometry(left, top, width, height)
self.result = QtWidgets.QLabel("result:")
self.setCentralWidget(self.result)
def create_dock_widgets(self):
self.results_window = results_window.Results_Window()
self.results_window.resultChanged.connect(self.result.setText)
self.resultsWindowDock = QtWidgets.QDockWidget("Results Viewer", self)
self.resultsWindowDock.setWidget(self.results_window )
self.resultsWindowDock.setFloating(False)
self.resultsWindowDock.setVisible(True)
self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.resultsWindowDock)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
I had similar problem in PyQT5 where I was unable to access and set the local variables. After a lot of struggle I found writing to file and reading from file as the best solution. Simply write the desired output to file and access the same info from other file. Works great!

Categories