First I'm using python3.7. I first made my gui with qt designer(pyqt5) and modified it a little bit to match my needs. What I want is that whenever a user tries to close the window, a pop-up message will ask if he's sure.
I searched all over the internet but the only conclusion I got to is that: I'm using this main function:
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
ui = Ui_main_window(window)
ui.setup_ui(window)
window.show()
sys.exit(app.exec_())
And the answers I found online are using the main function:
app = QtWidgets.QApplication(sys.argv)
ui = Ui_main_window()
ui.show()
sys.exit(app.exec_())
The differnece is that I .show()
the window
, and they .show()
the ui
.
In addition, I call the setup_ui(window)
with window
that I define with QMainWindow
.
I tried to merge the codes in various ways but unsuccessfully.
Here's my whole gui(on_enter
and print_from_server
aren't related):
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_main_window(object):
def __init__(self, main_window):
self.central_widget = QtWidgets.QWidget(main_window)
self.messages_spot = QtWidgets.QTextEdit(self.central_widget)
self.typing_spot = QtWidgets.QLineEdit(self.central_widget)
self.add_file = QtWidgets.QPushButton(self.central_widget)
self.Enter = QtWidgets.QPushButton(self.central_widget)
self.label = QtWidgets.QLabel(self.central_widget)
self.on_enter_handler = None
def setup_ui(self, main_window):
main_window.setObjectName("main_window")
main_window.setWindowModality(QtCore.Qt.NonModal)
main_window.setEnabled(True)
main_window.resize(800, 600)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(main_window.sizePolicy().hasHeightForWidth())
main_window.setSizePolicy(sizePolicy)
main_window.setMinimumSize(QtCore.QSize(800, 600))
main_window.setMaximumSize(QtCore.QSize(800, 600))
main_window.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("zt.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
main_window.setWindowIcon(icon)
main_window.setAutoFillBackground(False)
main_window.setInputMethodHints(QtCore.Qt.ImhNone)
main_window.setDocumentMode(False)
self.central_widget.setObjectName("central_widget")
self.messages_spot.setGeometry(QtCore.QRect(10, 60, 731, 461))
self.messages_spot.setUndoRedoEnabled(False)
self.messages_spot.setReadOnly(True)
consolas_font = QtGui.QFont()
consolas_font.setFamily("Consolas")
self.messages_spot.setFont(consolas_font)
self.messages_spot.setObjectName("messages_spot")
self.typing_spot.setGeometry(QtCore.QRect(10, 529, 691, 38))
self.typing_spot.setFont(consolas_font)
self.typing_spot.setObjectName("typing_spot")
self.add_file.setGeometry(QtCore.QRect(700, 529, 41, 38))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.add_file.sizePolicy().hasHeightForWidth())
self.add_file.setSizePolicy(sizePolicy)
self.add_file.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.add_file.setFocusPolicy(QtCore.Qt.WheelFocus)
self.add_file.setText("")
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("download.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.add_file.setIcon(icon1)
self.add_file.setIconSize(QtCore.QSize(37, 900))
self.add_file.setObjectName("add_file")
self.Enter.setGeometry(QtCore.QRect(410, 430, 75, 23))
self.Enter.setObjectName("Enter")
self.label.setGeometry(QtCore.QRect(10, 10, 731, 41))
font = QtGui.QFont()
font.setFamily("Gill Sans Ultra Bold Condensed")
font.setPointSize(12)
self.label.setFont(font)
self.label.setObjectName("label")
self.Enter.raise_()
self.typing_spot.raise_()
self.add_file.raise_()
self.messages_spot.raise_()
self.label.raise_()
main_window.setCentralWidget(self.central_widget)
self.Enter.clicked.connect(lambda: self.on_enter())
self.typing_spot.returnPressed.connect(self.Enter.click)
self.retranslate_ui(main_window)
QtCore.QMetaObject.connectSlotsByName(main_window)
def retranslate_ui(self, main_window):
_translate = QtCore.QCoreApplication.translate
main_window.setWindowTitle(_translate("main_window", "ZoomieTalks"))
self.Enter.setText(_translate("main_window", "Enter"))
self.label.setText(_translate("main_window", "<html><head/><body><p align=\"center\"><span style=\" font-size:28pt;\">ZoomieTalks</span></p></body></html>"))
def on_enter(self):
"""Executing protocol to send messages each time the Enter button is clicked"""
text = self.typing_spot.text()
if text.isspace() is False and text != '':
# self.to_server = text
if self.on_enter_handler is not None:
print(f"calling on_enter_handler({text})")
self.on_enter_handler(text)
# self.messages_spot.insertPlainText(f'• You: {text}\r\n')
self.messages_spot.verticalScrollBar().setValue(self.messages_spot.verticalScrollBar().maximum())
self.typing_spot.clear()
def print_from_server(self, text):
"""Printing messages from server in GUI"""
if text.isspace() is False and text != '':
print(f"printing from server: {text}")
self.messages_spot.insertPlainText(f'• {text}\r\n')
self.messages_spot.verticalScrollBar().setValue(self.messages_spot.verticalScrollBar().maximum())
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
ui = Ui_main_window(window)
ui.setup_ui(window)
window.show()
sys.exit(app.exec_())