Qt5 QComboBox focusOutEvent - python

I have built a GUI and among other things I am using a QComboBox (drop-down menu) with three entries. I want to enable/disable some other boxes and labels in the GUI, depending on which entry is selected in the QComboBox. I have managed to do that so far.
My problem is that all changes are taken into effect only when I press the "Run" button, which executes the application.
What I want to do is the action of enabling/disabling the relevant boxes will happen once I have selected the entry from the drop-down menu and not waiting until I press the "Run" button.
Is there a way to do something like that? Can I use the focusOutEvent to check when the user has clicked anywhere else than the drop-menu and apply the changes in the other boxes/labels?
I put below the class "controlling" my GUI, but I dont post the actual class generating the GUI because it is too lengthy. If you think it is relevant I can post it. I post instead a picture of the GUI.GUI The boxes and labels in the black circles should be disabled when the "Measuring Mode" drop-box is set to "Cable loss" or "Amplifier". When it is set to "Mixer" they should be enabled. I enable/disable the boxes and labels with an if statement inside the user_input function, in the Window class. The wanted drop-down menu is represeted by the "mode_select" variable.
Edit:
I think my explanation is not very intuitive without the actual gui code so I post it below.
from PyQt5 import QtWidgets, QtCore
from converter import convert_ui
import sys
import os
import time
class Window(QtWidgets.QMainWindow):
def __init__(self):
from transfer_function_gui import Ui_MainWindow
QtWidgets.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self) # setup GUI
self.ui.run_button_PM_tab.clicked.connect(self.user_input)
def user_input(self):
test_mode = self.ui.mode_select.currentText() # type: str
if test_mode == "Mixer":
self.ui.LO_Power_input.setEnabled(True)
self.ui.LO_Power_lbl.setEnabled(True)
self.ui.LO_freq_input.setEnabled(True)
self.ui.LO_freq_lbl.setEnabled(True)
else:
self.ui.LO_Power_input.setEnabled(False)
self.ui.LO_Power_lbl.setEnabled(False)
self.ui.LO_freq_input.setEnabled(False)
self.ui.LO_freq_lbl.setEnabled(False)
############################### Main ###############################
if __name__=='__main__':
convert_ui('transfer_function_gui.ui')
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
window = Window()
window.show()
sys.exit(app.exec_())
GUI generation (from Qt designer)
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(770, 613)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
self.progressBar.setGeometry(QtCore.QRect(10, 520, 630, 30))
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.Dev_address_frame = QtWidgets.QGroupBox(self.centralwidget)
self.Dev_address_frame.setGeometry(QtCore.QRect(10, 10, 371, 140))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Maximum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.Dev_address_frame.sizePolicy().hasHeightForWidth())
self.Dev_address_frame.setSizePolicy(sizePolicy)
self.Dev_address_frame.setMinimumSize(QtCore.QSize(0, 100))
font = QtGui.QFont()
font.setPointSize(8)
self.Dev_address_frame.setFont(font)
self.Dev_address_frame.setFlat(False)
self.Dev_address_frame.setCheckable(False)
self.Dev_address_frame.setObjectName("Dev_address_frame")
self.SigGen_label = QtWidgets.QLabel(self.Dev_address_frame)
self.SigGen_label.setGeometry(QtCore.QRect(20, 30, 100, 20))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.SigGen_label.sizePolicy().hasHeightForWidth())
self.SigGen_label.setSizePolicy(sizePolicy)
self.SigGen_label.setObjectName("SigGen_label")
self.PM_label = QtWidgets.QLabel(self.Dev_address_frame)
self.PM_label.setGeometry(QtCore.QRect(20, 70, 100, 20))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Maximum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.PM_label.sizePolicy().hasHeightForWidth())
self.PM_label.setSizePolicy(sizePolicy)
self.PM_label.setObjectName("PM_label")
self.SigGen_addr_input = QtWidgets.QLineEdit(self.Dev_address_frame)
self.SigGen_addr_input.setGeometry(QtCore.QRect(120, 30, 180, 20))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Maximum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.SigGen_addr_input.sizePolicy().hasHeightForWidth())
self.SigGen_addr_input.setSizePolicy(sizePolicy)
self.SigGen_addr_input.setObjectName("SigGen_addr_input")
self.PM_addr_input = QtWidgets.QLineEdit(self.Dev_address_frame)
self.PM_addr_input.setGeometry(QtCore.QRect(120, 70, 180, 20))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Maximum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.PM_addr_input.sizePolicy().hasHeightForWidth())
self.PM_addr_input.setSizePolicy(sizePolicy)
self.PM_addr_input.setObjectName("PM_addr_input")
self.PM_cnct_checkBox = QtWidgets.QCheckBox(self.Dev_address_frame)
self.PM_cnct_checkBox.setEnabled(False)
self.PM_cnct_checkBox.setGeometry(QtCore.QRect(240, 105, 16, 20))
self.PM_cnct_checkBox.setText("")
self.PM_cnct_checkBox.setObjectName("PM_cnct_checkBox")
self.PM_cnct_lbl = QtWidgets.QLabel(self.Dev_address_frame)
self.PM_cnct_lbl.setEnabled(False)
self.PM_cnct_lbl.setGeometry(QtCore.QRect(20, 105, 210, 20))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Maximum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.PM_cnct_lbl.sizePolicy().hasHeightForWidth())
self.PM_cnct_lbl.setSizePolicy(sizePolicy)
self.PM_cnct_lbl.setObjectName("PM_cnct_lbl")
self.Freq_setup_frame = QtWidgets.QGroupBox(self.centralwidget)
self.Freq_setup_frame.setGeometry(QtCore.QRect(10, 200, 370, 181))
font = QtGui.QFont()
font.setPointSize(8)
self.Freq_setup_frame.setFont(font)
self.Freq_setup_frame.setFlat(False)
self.Freq_setup_frame.setCheckable(False)
self.Freq_setup_frame.setObjectName("Freq_setup_frame")
self.Start_freq_lbl = QtWidgets.QLabel(self.Freq_setup_frame)
self.Start_freq_lbl.setEnabled(True)
self.Start_freq_lbl.setGeometry(QtCore.QRect(20, 30, 100, 20))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.Start_freq_lbl.sizePolicy().hasHeightForWidth())
self.Start_freq_lbl.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(8)
self.Start_freq_lbl.setFont(font)
self.Start_freq_lbl.setMidLineWidth(0)
self.Start_freq_lbl.setText("Start freq. ")
self.Start_freq_lbl.setObjectName("Start_freq_lbl")
self.Stop_freq_lbl = QtWidgets.QLabel(self.Freq_setup_frame)
self.Stop_freq_lbl.setGeometry(QtCore.QRect(20, 70, 100, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.Stop_freq_lbl.setFont(font)
self.Stop_freq_lbl.setObjectName("Stop_freq_lbl")
self.Step_freq_lbl = QtWidgets.QLabel(self.Freq_setup_frame)
self.Step_freq_lbl.setGeometry(QtCore.QRect(20, 110, 100, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.Step_freq_lbl.setFont(font)
self.Step_freq_lbl.setObjectName("Step_freq_lbl")
self.StopFreq_input = QtWidgets.QLineEdit(self.Freq_setup_frame)
self.StopFreq_input.setGeometry(QtCore.QRect(120, 70, 180, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.StopFreq_input.setFont(font)
self.StopFreq_input.setText("")
self.StopFreq_input.setObjectName("StopFreq_input")
self.Start_freq_input = QtWidgets.QLineEdit(self.Freq_setup_frame)
self.Start_freq_input.setGeometry(QtCore.QRect(120, 30, 180, 20))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Maximum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.Start_freq_input.sizePolicy().hasHeightForWidth())
self.Start_freq_input.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(8)
self.Start_freq_input.setFont(font)
self.Start_freq_input.setText("")
self.Start_freq_input.setObjectName("Start_freq_input")
self.StepFreq_input = QtWidgets.QLineEdit(self.Freq_setup_frame)
self.StepFreq_input.setGeometry(QtCore.QRect(120, 110, 180, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.StepFreq_input.setFont(font)
self.StepFreq_input.setText("")
self.StepFreq_input.setObjectName("StepFreq_input")
self.freq_units_select = QtWidgets.QComboBox(self.Freq_setup_frame)
self.freq_units_select.setGeometry(QtCore.QRect(310, 29, 51, 141))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.freq_units_select.sizePolicy().hasHeightForWidth())
self.freq_units_select.setSizePolicy(sizePolicy)
self.freq_units_select.setMinimumSize(QtCore.QSize(0, 70))
font = QtGui.QFont()
font.setPointSize(8)
self.freq_units_select.setFont(font)
self.freq_units_select.setInsertPolicy(QtWidgets.QComboBox.InsertAtBottom)
self.freq_units_select.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContentsOnFirstShow)
self.freq_units_select.setFrame(True)
self.freq_units_select.setObjectName("freq_units_select")
self.freq_units_select.addItem("")
self.freq_units_select.addItem("")
self.freq_units_select.addItem("")
self.LO_freq_lbl = QtWidgets.QLabel(self.Freq_setup_frame)
self.LO_freq_lbl.setGeometry(QtCore.QRect(20, 150, 100, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.LO_freq_lbl.setFont(font)
self.LO_freq_lbl.setObjectName("LO_freq_lbl")
self.LO_freq_input = QtWidgets.QLineEdit(self.Freq_setup_frame)
self.LO_freq_input.setGeometry(QtCore.QRect(120, 150, 180, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.LO_freq_input.setFont(font)
self.LO_freq_input.setText("")
self.LO_freq_input.setObjectName("LO_freq_input")
self.Power_setup_frame = QtWidgets.QGroupBox(self.centralwidget)
self.Power_setup_frame.setGeometry(QtCore.QRect(10, 390, 370, 111))
font = QtGui.QFont()
font.setPointSize(8)
self.Power_setup_frame.setFont(font)
self.Power_setup_frame.setFlat(False)
self.Power_setup_frame.setCheckable(False)
self.Power_setup_frame.setObjectName("Power_setup_frame")
self.DUT_input_lbl = QtWidgets.QLabel(self.Power_setup_frame)
self.DUT_input_lbl.setGeometry(QtCore.QRect(20, 30, 100, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.DUT_input_lbl.setFont(font)
self.DUT_input_lbl.setObjectName("DUT_input_lbl")
self.LO_Power_lbl = QtWidgets.QLabel(self.Power_setup_frame)
self.LO_Power_lbl.setGeometry(QtCore.QRect(20, 70, 100, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.LO_Power_lbl.setFont(font)
self.LO_Power_lbl.setObjectName("LO_Power_lbl")
self.DUT_Input_power_input = QtWidgets.QLineEdit(self.Power_setup_frame)
self.DUT_Input_power_input.setGeometry(QtCore.QRect(120, 30, 180, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.DUT_Input_power_input.setFont(font)
self.DUT_Input_power_input.setText("")
self.DUT_Input_power_input.setObjectName("DUT_Input_power_input")
self.LO_Power_input = QtWidgets.QLineEdit(self.Power_setup_frame)
self.LO_Power_input.setGeometry(QtCore.QRect(120, 70, 180, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.LO_Power_input.setFont(font)
self.LO_Power_input.setText("")
self.LO_Power_input.setObjectName("LO_Power_input")
self.LO_dBm = QtWidgets.QLabel(self.Power_setup_frame)
self.LO_dBm.setGeometry(QtCore.QRect(320, 70, 20, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.LO_dBm.setFont(font)
self.LO_dBm.setObjectName("LO_dBm")
self.DUT_dBm = QtWidgets.QLabel(self.Power_setup_frame)
self.DUT_dBm.setGeometry(QtCore.QRect(320, 30, 20, 20))
font = QtGui.QFont()
font.setPointSize(8)
self.DUT_dBm.setFont(font)
self.DUT_dBm.setObjectName("DUT_dBm")
self.mode_label = QtWidgets.QLabel(self.centralwidget)
self.mode_label.setGeometry(QtCore.QRect(30, 165, 100, 20))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.mode_label.sizePolicy().hasHeightForWidth())
self.mode_label.setSizePolicy(sizePolicy)
self.mode_label.setBaseSize(QtCore.QSize(0, 0))
self.mode_label.setObjectName("mode_label")
self.mode_select = QtWidgets.QComboBox(self.centralwidget)
self.mode_select.setGeometry(QtCore.QRect(130, 165, 180, 20))
self.mode_select.setObjectName("mode_select")
self.mode_select.addItem("")
self.mode_select.addItem("")
self.mode_select.addItem("")
self.run_button_PM_tab = QtWidgets.QPushButton(self.centralwidget)
self.run_button_PM_tab.setEnabled(True)
self.run_button_PM_tab.setGeometry(QtCore.QRect(650, 520, 100, 30))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.run_button_PM_tab.sizePolicy().hasHeightForWidth())
self.run_button_PM_tab.setSizePolicy(sizePolicy)
self.run_button_PM_tab.setMinimumSize(QtCore.QSize(0, 0))
self.run_button_PM_tab.setLayoutDirection(QtCore.Qt.LeftToRight)
self.run_button_PM_tab.setObjectName("run_button_PM_tab")
self.S21_table = QtWidgets.QTableWidget(self.centralwidget)
self.S21_table.setGeometry(QtCore.QRect(400, 10, 360, 490))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(33)
sizePolicy.setVerticalStretch(25)
sizePolicy.setHeightForWidth(self.S21_table.sizePolicy().hasHeightForWidth())
self.S21_table.setSizePolicy(sizePolicy)
self.S21_table.setMinimumSize(QtCore.QSize(15, 35))
self.S21_table.setSizeIncrement(QtCore.QSize(0, 0))
self.S21_table.setBaseSize(QtCore.QSize(0, 0))
self.S21_table.setObjectName("S21_table")
self.S21_table.setColumnCount(2)
self.S21_table.setRowCount(1)
item = QtWidgets.QTableWidgetItem()
self.S21_table.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
item.setTextAlignment(QtCore.Qt.AlignCenter)
self.S21_table.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.S21_table.setHorizontalHeaderItem(1, item)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 770, 21))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuAbout = QtWidgets.QMenu(self.menubar)
self.menuAbout.setObjectName("menuAbout")
self.menuPreferences = QtWidgets.QMenu(self.menubar)
self.menuPreferences.setObjectName("menuPreferences")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionSave_as = QtWidgets.QAction(MainWindow)
self.actionSave_as.setObjectName("actionSave_as")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.menuFile.addAction(self.actionSave_as)
self.menuFile.addAction(self.actionExit)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuPreferences.menuAction())
self.menubar.addAction(self.menuAbout.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Transfer Function Calculator v0.5"))
self.Dev_address_frame.setTitle(_translate("MainWindow", "Device addresses (GPIB, USB, TCPIP)"))
self.SigGen_label.setText(_translate("MainWindow", "Signal Generator"))
self.PM_label.setText(_translate("MainWindow", "Power Meter"))
self.PM_cnct_lbl.setText(_translate("MainWindow", "If PM is connected to SigGen check the box"))
self.Freq_setup_frame.setTitle(_translate("MainWindow", "Frequency setup"))
self.Stop_freq_lbl.setText(_translate("MainWindow", "Stop freq."))
self.Step_freq_lbl.setText(_translate("MainWindow", "Step freq."))
self.freq_units_select.setCurrentText(_translate("MainWindow", "kHz"))
self.freq_units_select.setItemText(0, _translate("MainWindow", "kHz"))
self.freq_units_select.setItemText(1, _translate("MainWindow", "MHz"))
self.freq_units_select.setItemText(2, _translate("MainWindow", "GHz"))
self.LO_freq_lbl.setText(_translate("MainWindow", "LO freq."))
self.Power_setup_frame.setTitle(_translate("MainWindow", "Power setup"))
self.DUT_input_lbl.setText(_translate("MainWindow", "DUT Input power"))
self.LO_Power_lbl.setText(_translate("MainWindow", "LO Power"))
self.LO_dBm.setText(_translate("MainWindow", "dBm"))
self.DUT_dBm.setText(_translate("MainWindow", "dBm"))
self.mode_label.setText(_translate("MainWindow", "Measuring mode"))
self.mode_select.setItemText(0, _translate("MainWindow", "Cable loss"))
self.mode_select.setItemText(1, _translate("MainWindow", "Amplifier"))
self.mode_select.setItemText(2, _translate("MainWindow", "Mixer"))
self.run_button_PM_tab.setText(_translate("MainWindow", "Run"))
item = self.S21_table.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "New Row"))
item = self.S21_table.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "New Column"))
item = self.S21_table.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "New Column"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuAbout.setTitle(_translate("MainWindow", "About"))
self.menuPreferences.setTitle(_translate("MainWindow", "Settings..."))
self.actionSave_as.setText(_translate("MainWindow", "Save as..."))
self.actionExit.setText(_translate("MainWindow", "Exit"))

QComboBox uses these signals here which you can connect using signals and slots.
I think the one you would want to use is either currentTextChanged or currentIndexChanged. So you could do something like this in your init:
class Window(QtWidgets.QMainWindow):
def __init__(self):
from transfer_function_gui import Ui_MainWindow
QtWidgets.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self) # setup GUI
self.ui.run_button_PM_tab.clicked.connect(self.user_input)
self.ui.freq_units_select.currentIndexChanged.connect(self.user_input)
self.ui.mode_select.currentIndexChanged.connect(self.user_input)
This will connect the event of the QComboBox changing it's selected index to a method called self.user_input.

Related

How pass data from second window to main?

I have a python app that change mac address with pyqt5 GUI. I have two python files. The main python file (mainWindow.py) that run main program then I press button to open the second window from second python file secondWindow.py. In second python file opens a window entry then I have to put letters and numbers like then I have to pass data in the first main app label window.
Can anyone help me to solve that?
I tried to reduce code.
mainWindow.py
#!/usr/bin/python3
import os
import sys
import subprocess
from PyQt5 import QtCore, QtGui, QtWidgets
from secondWindow import Ui_macSpecific
import macspoof_bash
import settings
macspoof_bash.write_to_ini()
# Read and write settings in macspoof.ini file
from configparser import ConfigParser
# instantiate
config = ConfigParser()
# parse existing file
config.read('/home/user/.config/macspoof/macspoof.ini')
# read values from a section
wired = config.get('ethernet', 'wired')
wireless = config.get('wifi', 'wireless')
class Ui_MainWindow(object):
def specificWindow(self):
self.window = QtWidgets.QDialog()
self.ui = Ui_macSpecific()
#self.ui.setupUi(self.window)
self.ui.setupUi(self.window, self) # <--- assign MainWindow as parent to second window
self.window.show()
def aboutWindow(self):
self.aboutWindow = QtWidgets.QDialog()
self.ui = Ui_Dialog()
self.ui.setupUi(self.aboutWindow)
self.aboutWindow.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(480, 400)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setStyleSheet("background-color: rgb(36, 31, 49);\n"
"font: 11pt \"DejaVu Sans\";\n"
"color: rgb(246, 245, 244);")
self.label_3.setAlignment(QtCore.Qt.AlignCenter)
self.label_3.setObjectName("label_3")
self.gridLayout.addWidget(self.label_3, 1, 0, 1, 1)
self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: self.specificWindow())
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_6.sizePolicy().hasHeightForWidth())
self.pushButton_6.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("DejaVu Sans")
font.setPointSize(10)
self.pushButton_6.setFont(font)
self.pushButton_6.setObjectName("pushButton_6")
self.gridLayout.addWidget(self.pushButton_6, 4, 1, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: self.button_vendor_wired())
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())
self.pushButton.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("DejaVu Sans")
font.setPointSize(10)
self.pushButton.setFont(font)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 2, 0, 1, 1)
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: self.button_fully_wired())
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_3.sizePolicy().hasHeightForWidth())
self.pushButton_3.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("DejaVu Sans")
font.setPointSize(10)
self.pushButton_3.setFont(font)
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout.addWidget(self.pushButton_3, 3, 0, 1, 1)
self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: self.button_original_wireless())
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_8.sizePolicy().hasHeightForWidth())
self.pushButton_8.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("DejaVu Sans")
font.setPointSize(10)
self.pushButton_8.setFont(font)
self.pushButton_8.setLayoutDirection(QtCore.Qt.LeftToRight)
self.pushButton_8.setAutoFillBackground(False)
self.pushButton_8.setObjectName("pushButton_8")
self.gridLayout.addWidget(self.pushButton_8, 5, 1, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: self.button_vendor_wireless())
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_2.sizePolicy().hasHeightForWidth())
self.pushButton_2.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("DejaVu Sans")
font.setPointSize(10)
self.pushButton_2.setFont(font)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 2, 1, 1, 1)
self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: self.button_original_wired())
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_7.sizePolicy().hasHeightForWidth())
self.pushButton_7.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("DejaVu Sans")
font.setPointSize(10)
self.pushButton_7.setFont(font)
self.pushButton_7.setObjectName("pushButton_7")
self.gridLayout.addWidget(self.pushButton_7, 5, 0, 1, 1)
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: self.specificWindow())
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_5.sizePolicy().hasHeightForWidth())
self.pushButton_5.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("DejaVu Sans")
font.setPointSize(10)
self.pushButton_5.setFont(font)
self.pushButton_5.setObjectName("pushButton_5")
self.gridLayout.addWidget(self.pushButton_5, 4, 0, 1, 1)
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setStyleSheet("background-color: rgb(36, 31, 49);\n"
"font: 11pt \"DejaVu Sans\";\n"
"color: rgb(246, 245, 244);")
self.label_4.setObjectName("label_4")
self.label_4.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.label_4, 1, 1, 1, 1)
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget, clicked = lambda: self.button_fully_wireless())
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_4.sizePolicy().hasHeightForWidth())
self.pushButton_4.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("DejaVu Sans")
font.setPointSize(10)
self.pushButton_4.setFont(font)
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout.addWidget(self.pushButton_4, 3, 1, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setMinimumSize(QtCore.QSize(0, 0))
self.label.setSizeIncrement(QtCore.QSize(0, 0))
self.label.setStyleSheet("background-color: rgb(36, 31, 49);\n"
"font: 11pt \"DejaVu Sans\";\n"
"color: rgb(246, 245, 244);")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setStyleSheet("background-color: rgb(36, 31, 49);\n"
"font: 11pt \"DejaVu Sans\";\n"
"color: rgb(246, 245, 244);")
self.label_2.setObjectName("label_2")
self.label_2.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout.addWidget(self.label_2, 0, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 480, 20))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuEdit = QtWidgets.QMenu(self.menubar)
self.menuEdit.setObjectName("menuEdit")
self.menuHelp = QtWidgets.QMenu(self.menubar)
self.menuHelp.setObjectName("menuHelp")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.triggered.connect(lambda: self.close_window())
self.actionExit.setObjectName("actionExit")
self.actionPreferences = QtWidgets.QAction(MainWindow)
self.actionPreferences.setObjectName("actionPreferences")
self.actionAbout = QtWidgets.QAction(MainWindow)
self.actionAbout.triggered.connect(lambda: self.aboutWindow())
self.actionAbout.setObjectName("actionAbout")
self.menuFile.addAction(self.actionExit)
self.menuEdit.addAction(self.actionPreferences)
self.menuHelp.addAction(self.actionAbout)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuEdit.menuAction())
self.menubar.addAction(self.menuHelp.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.update_wired_label(MainWindow)
self.update_wired_mac_address_label(MainWindow)
self.update_wireless_label(MainWindow)
self.update_wireless_mac_address_label(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MAC address spoofing"))
self.label_3.setText(_translate("MainWindow", "TextLabel"))
self.pushButton_6.setText(_translate("MainWindow", "Set the MAC"))
self.pushButton.setText(_translate("MainWindow", "Don\'t change vendor bytes"))
self.pushButton_3.setText(_translate("MainWindow", "Set fully random MAC"))
self.pushButton_8.setText(_translate("MainWindow", "Reset MAC address to its original"))
self.pushButton_2.setText(_translate("MainWindow", "Don\'t change vendor bytes"))
self.pushButton_7.setText(_translate("MainWindow", "Reset MAC address to its original"))
self.pushButton_5.setText(_translate("MainWindow", "Set the MAC"))
self.label_4.setText(_translate("MainWindow", "TextLabel"))
self.pushButton_4.setText(_translate("MainWindow", "Set fully random MAC"))
self.label.setText(_translate("MainWindow", "TextLabel"))
self.label_2.setText(_translate("MainWindow", "TextLabel"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuEdit.setTitle(_translate("MainWindow", "Settings"))
self.menuHelp.setTitle(_translate("MainWindow", "Help"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
self.actionPreferences.setText(_translate("MainWindow", "Preferences"))
self.actionAbout.setText(_translate("MainWindow", "About"))
def update_wired_label(self, MainWindow):
print('update_wired_label')
self.wired = config.get('ethernet', 'wired')
self.label.setText(str(self.wired))
def update_wired_mac_address_label(self, MainWindow):
print('update_wired_mac_address_label')
config.read('/home/user/.config/macspoof/macspoof.ini')
self.wired_mac_address = config.get('ethernet', 'wired_mac_address')
self.label_3.setText(str(self.wired_mac_address))
def update_wireless_label(self, MainWindow):
print('update_wireless_label')
self.wireless = config.get('wifi', 'wireless')
self.label_2.setText(str(self.wireless))
def update_wireless_mac_address_label(self, MainWindow):
print('update_wireless_mac_address_label')
config.read('/home/user/.config/macspoof/macspoof.ini')
self.wireless_mac_address = config.get('wifi', 'wireless_mac_address')
self.label_4.setText(str(self.wireless_mac_address))
def button_vendor_wired(self):
print('vendor_wired')
subprocess.run(['macchanger', '-e', wired])
macspoof_bash.vendor_wired()
self.update_wired_mac_address_label(MainWindow)
def button_fully_wired(self):
print('fully wired')
subprocess.run(['macchanger', '-r', wired])
macspoof_bash.write_to_ini()
self.update_wired_mac_address_label(MainWindow)
#def button_specific_wired(self):
# print('specific wired')
# subprocess.run(["macchanger", "-m", wired])
def button_original_wired(self):
print('original wired')
subprocess.run(['macchanger', '-p', wired])
macspoof_bash.write_to_ini()
self.update_wired_mac_address_label(MainWindow)
def button_vendor_wireless(self):
print('vendor_wireless')
subprocess.run(['macchanger', '-e', wireless])
macspoof_bash.vendor_wireless()
self.update_wireless_mac_address_label(MainWindow)
def button_fully_wireless(self):
print('fully wireless')
#wireless = config.get("wifi", "wireless")
#subprocess.run(['macchanger', '-r', wired])
macspoof_bash.fully_wireless()
self.update_wireless_mac_address_label(MainWindow)
def button_specific_wireless(self):
print('specific wired')
subprocess.run(["macchanger", "-m", wireless])
def button_original_wireless(self):
print('original wireless')
subprocess.run(['macchanger', '-p', wireless])
macspoof_bash.write_to_ini()
self.update_wireless_mac_address_label(MainWindow)
def close_window(self):
print("Quit")
sys.exit()
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_())
secondWindow.py
from PyQt5 import QtCore, QtGui, QtWidgets
# Read and write settings in macspoof.ini file
from configparser import ConfigParser
# instantiate
config = ConfigParser()
# parse existing file
config.read('macspoof.ini')
# read values from a section
wired = config.get('ethernet', 'wired')
wired_mac_address = config.get('ethernet', 'wired_mac_address')
class Ui_macSpecific(object):
def setupUi(self, macSpecific, parent): # <--- add extra parameter parent
self.parent = parent # < --- create new attribute parent
#def setupUi(self, macSpecific):
macSpecific.setObjectName("macSpecific")
macSpecific.resize(301, 140)
self.gridLayout = QtWidgets.QGridLayout(macSpecific)
self.gridLayout.setObjectName("gridLayout")
self.label = QtWidgets.QLabel(macSpecific)
font = QtGui.QFont()
font.setFamily("DejaVu Sans")
font.setPointSize(10)
self.label.setFont(font)
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.lineEdit = QtWidgets.QLineEdit(macSpecific)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth())
self.lineEdit.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setFamily("DejaVu Sans")
font.setPointSize(14)
self.lineEdit.setFont(font)
self.lineEdit.setLayoutDirection(QtCore.Qt.LeftToRight)
self.lineEdit.setAutoFillBackground(False)
self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
self.lineEdit.setObjectName("lineEdit")
self.gridLayout.addWidget(self.lineEdit, 1, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(macSpecific, clicked = lambda: self.submit())
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 2, 0, 1, 1)
self.retranslateUi(macSpecific)
QtCore.QMetaObject.connectSlotsByName(macSpecific)
def retranslateUi(self, macSpecific):
_translate = QtCore.QCoreApplication.translate
macSpecific.setWindowTitle(_translate("macSpecific", "MAC address spoofing"))
self.label.setText(_translate("macSpecific", "Enter A MAC address"))
self.lineEdit.setText(_translate("macSpecific", "11:22:33:44:55:66"))
self.pushButton.setText(_translate("macSpecific", "Submit"))
def submit(self):
print("submit")
#self.parent.label_3.setText(str(self.wired_mac_address)) # use parent to set label text
self.parent.label_3.setText(str(self.parent.wired_mac_address)) # <-----Try this then.
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
macSpecific = QtWidgets.QDialog()
ui = Ui_macSpecific()
ui.setupUi(macSpecific)
macSpecific.show()
sys.exit(app.exec_())
One solution would be to assign the MainWindow as the parent of the SecondWindow. Then to get the label text all the SecondWindow has to do is self.parent.label.setText('something'). See the minor adjustments I made below.
mainWindow.py
class Ui_MainWindow(object):
def specificWindow(self):
self.window = QtWidgets.QDialog()
self.ui = Ui_macSpecific()
self.ui.setupUi(self.window, self) # <--- assign MainWindow as parent to second window
self.window.show()
secondWindow.py
import macspoof_bash
class Ui_macSpecific(object):
def setupUi(self, macSpecific, master):
self.master = master
self.window = macSpecific
macSpecific.setObjectName("macSpecific")
macSpecific.resize(301, 140)
...
...
self.pushButton = QtWidgets.QPushButton(macSpecific, clicked = lambda: self.submit())
def submit(self):
print("submit")
text = self.lineEdit.text()
self.master.label_3.setText(text)
self.window.close()
config['ethernet']['wired_mac_address'] = text
with open("macspoof.ini", "w") as inifile:
config.write(inifile)

How to close a window in Pyqt5 [duplicate]

This question already has answers here:
QtDesigner changes will be lost after redesign User Interface
(2 answers)
How to close a QDialog
(4 answers)
Closed 1 year ago.
I've created a program that allows you to login and sign up. When you have the right credentials it opens a window but it doesn't close. The same thing happens when you press the button to sign up. I've tried to use the close method but it doesn't work i always get:
AttributeError: type object 'Ui_Dialog' has no attribute 'close'
here is the full code of all the windows:
Login Page
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QMessageBox
import sqlite3
from welcome import Ui_MainWindow
from signup_page import Ui_Dialog2
import sys
#widget.setCurrentIndex(widget.currentIndex)
class Ui_Dialog(object):
def showMessageBox(self):
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(0, 0, 0);\n"
"background-color: rgb(255, 0, 0);")
self.label.setObjectName("label")
self.label.setText("WRONG USERNAME/PASSWORD")
def loginCheck(self):
username = self.username_input.text()
password = self.password_input.text()
connection = sqlite3.connect("login.db")
result = connection.execute("SELECT * FROM USERS WHERE USERNAME = ? AND PASSWORD = ?", (username, password))
if (len(result.fetchall())) > 0:
self.welcomewindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.welcomewindow)
self.welcomewindow.show()
else:
self.showMessageBox()
def singup(self):
self.signup_page = QtWidgets.QDialog()
self.ui = Ui_Dialog2()
self.ui.setupUi(self.signup_page)
self.signup_page.show()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(591, 457)
self.user_name_label = QtWidgets.QLabel(Dialog)
self.user_name_label.setGeometry(QtCore.QRect(190, 150, 81, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.user_name_label.sizePolicy().hasHeightForWidth())
self.user_name_label.setSizePolicy(sizePolicy)
self.user_name_label.setSizeIncrement(QtCore.QSize(0, 0))
self.user_name_label.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(13)
self.user_name_label.setFont(font)
self.user_name_label.setObjectName("user_name_label")
self.password_label = QtWidgets.QLabel(Dialog)
self.password_label.setGeometry(QtCore.QRect(190, 210, 81, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.password_label.sizePolicy().hasHeightForWidth())
self.password_label.setSizePolicy(sizePolicy)
self.password_label.setSizeIncrement(QtCore.QSize(0, 0))
self.password_label.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(13)
self.password_label.setFont(font)
self.password_label.setObjectName("password_label")
self.username_input = QtWidgets.QLineEdit(Dialog)
self.username_input.setGeometry(QtCore.QRect(280, 150, 141, 20))
self.username_input.setObjectName("username_input")
self.password_input = QtWidgets.QLineEdit(Dialog)
self.password_input.setGeometry(QtCore.QRect(280, 210, 141, 20))
self.password_input.setObjectName("password_input")
self.password_input.setEchoMode(self.password_input.Password)
self.login_btn = QtWidgets.QPushButton(Dialog)
self.login_btn.setGeometry(QtCore.QRect(200, 260, 81, 31))
font = QtGui.QFont()
font.setPointSize(13)
self.login_btn.setFont(font)
self.login_btn.setObjectName("login_btn")
#########Button Event##########
self.login_btn.clicked.connect(self.loginCheck)
###############################
self.signup_btn = QtWidgets.QPushButton(Dialog)
self.signup_btn.setGeometry(QtCore.QRect(300, 260, 81, 31))
########Button Event##########
self.signup_btn.clicked.connect(self.singup)
##############################
font = QtGui.QFont()
font.setPointSize(13)
self.signup_btn.setFont(font)
self.signup_btn.setObjectName("signup_btn")
self.login_title = QtWidgets.QLabel(Dialog)
self.login_title.setGeometry(QtCore.QRect(240, 40, 91, 71))
font = QtGui.QFont()
font.setPointSize(20)
self.login_title.setFont(font)
self.login_title.setObjectName("login_title")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(130, 370, 391, 31))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Login Page"))
self.user_name_label.setText(_translate("Dialog", "Username"))
self.password_label.setText(_translate("Dialog", "Password"))
self.login_btn.setText(_translate("Dialog", "Login"))
self.signup_btn.setText(_translate("Dialog", "Sign Up"))
self.login_title.setText(_translate("Dialog", " Login"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QStackedWidget()
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Dialog()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
The main window:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(593, 458)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(200, 180, 221, 71))
font = QtGui.QFont()
font.setPointSize(40)
self.label.setFont(font)
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "Welcome"))
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_())
and the signup page:
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
from welcome import Ui_MainWindow
import re
from PyQt5.QtWidgets import QMessageBox
class Ui_Dialog2(object):
def showMessageBox_password(self):
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(0, 0, 0);\n"
"background-color: rgb(255, 0, 0);")
self.label.setObjectName("label")
self.label.setText("INVALID PASSWORD")
def showMessageBox_email(self):
font = QtGui.QFont()
font.setPointSize(20)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(0, 0, 0);\n"
"background-color: rgb(255, 0, 0);")
self.label.setObjectName("label")
self.label.setText(" INVALID EMAIL")
def insertData(self):
username = self.username_entry.text()
password = self.password_entry.text()
email = self.email_entry.text()
connection = sqlite3.connect("login.db")
regex = '^(\w|\.|\_|\-)+[#](\w|\_|\-|\.)+[.]\w{2,3}$'
valid_email = True
valid_password = True
if(re.search(regex, email)):
valid_email = True
else:
valid_email = False
if len(password) > 7:
valid_password = True
else:
valid_password = False
if valid_email == True and valid_password == True:
connection.execute("INSERT INTO USERS VALUES(?,?,?)", (username, email, password))
connection.commit()
self.welcomewindow = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.welcomewindow)
self.welcomewindow.show()
if valid_email == False:
self.showMessageBox_email()
elif valid_password == False:
self.showMessageBox_password()
connection.close()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(593, 458)
self.user_name_label = QtWidgets.QLabel(Dialog)
self.user_name_label.setGeometry(QtCore.QRect(190, 200, 81, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.user_name_label.sizePolicy().hasHeightForWidth())
self.user_name_label.setSizePolicy(sizePolicy)
self.user_name_label.setSizeIncrement(QtCore.QSize(0, 0))
self.user_name_label.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(13)
self.user_name_label.setFont(font)
self.user_name_label.setObjectName("user_name_label")
self.password_label = QtWidgets.QLabel(Dialog)
self.password_label.setGeometry(QtCore.QRect(190, 240, 81, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.password_label.sizePolicy().hasHeightForWidth())
self.password_label.setSizePolicy(sizePolicy)
self.password_label.setSizeIncrement(QtCore.QSize(0, 0))
self.password_label.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(13)
self.password_label.setFont(font)
self.password_label.setObjectName("password_label")
self.email_label = QtWidgets.QLabel(Dialog)
self.email_label.setGeometry(QtCore.QRect(190, 160, 81, 16))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.email_label.sizePolicy().hasHeightForWidth())
self.email_label.setSizePolicy(sizePolicy)
self.email_label.setSizeIncrement(QtCore.QSize(0, 0))
self.email_label.setBaseSize(QtCore.QSize(0, 0))
font = QtGui.QFont()
font.setPointSize(13)
self.email_label.setFont(font)
self.email_label.setObjectName("email_label")
self.email_entry = QtWidgets.QLineEdit(Dialog)
self.email_entry.setGeometry(QtCore.QRect(270, 160, 200, 20))
self.email_entry.setObjectName("email_entry")
self.username_entry = QtWidgets.QLineEdit(Dialog)
self.username_entry.setGeometry(QtCore.QRect(270, 200, 200, 20))
self.username_entry.setObjectName("username_entry")
self.password_entry = QtWidgets.QLineEdit(Dialog)
self.password_entry.setGeometry(QtCore.QRect(270, 240, 200, 20))
self.password_entry.setObjectName("password_entry")
self.password_entry.setEchoMode(self.password_entry.Password)
self.sign_up_button = QtWidgets.QPushButton(Dialog)
self.sign_up_button.setGeometry(QtCore.QRect(270, 280, 81, 31))
font = QtGui.QFont()
font.setPointSize(13)
self.sign_up_button.setFont(font)
self.sign_up_button.setObjectName("sign_up_button")
self.sign_up_button.clicked.connect(self.insertData)
self.login_title = QtWidgets.QLabel(Dialog)
self.login_title.setGeometry(QtCore.QRect(220, 50, 191, 71))
font = QtGui.QFont()
font.setPointSize(20)
self.login_title.setFont(font)
self.login_title.setObjectName("login_title")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(170, 360, 251, 31))
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Sign Up Page"))
self.user_name_label.setText(_translate("Dialog", "Username"))
self.password_label.setText(_translate("Dialog", "Password"))
self.email_label.setText(_translate("Dialog", "Email"))
self.sign_up_button.setText(_translate("Dialog", "Sign Up"))
self.login_title.setText(_translate("Dialog", "Create Account"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Dialog2()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

issue with QScrollArea

I am trying to add some check boxes in scroll area.but for me it is not working properly.i am new in python could anyone help?
import PyQt5
from PyQt5 import QtCore, QtGui, QtWidgets
from configparser import ConfigParser
import paramiko
import threading
import os
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
class Ui_MainWindow(object):
def __init__(self, parent=None):
self.configur = ConfigParser()
self.configur.read('ServerConfigurationDetails.ini')
self.serverName=self.configur.sections()
print(self.serverName)
self.getComponents = self.configur.options("Component")
self.sLen = len(self.serverName)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(951, 634)
font = QtGui.QFont()
font.setPointSize(10)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(150, 220, 91, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
self.comboBox_2.setGeometry(QtCore.QRect(250, 220, 231, 31))
self.comboBox_2.setObjectName("comboBox_2")
self.comboBox_2.addItem("")
self.comboBox_2.addItem("")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(700, 40, 101, 31))
self.pushButton.setObjectName("pushButton")
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(140, 260, 681, 81))
self.groupBox.setObjectName("groupBox")
self.radioButton = QtWidgets.QRadioButton(self.groupBox)
self.radioButton.setGeometry(QtCore.QRect(10, 30, 201, 20))
self.radioButton.setObjectName("radioButton")
self.radioButton_2 = QtWidgets.QRadioButton(self.groupBox)
self.radioButton_2.setGeometry(QtCore.QRect(220, 30, 201, 20))
self.radioButton_2.setObjectName("radioButton_2")
self.checkBox = QtWidgets.QCheckBox(self.groupBox)
self.checkBox.setEnabled(False)
self.checkBox.setGeometry(QtCore.QRect(10, 50, 231, 31))
self.checkBox.setChecked(True)
self.checkBox.setObjectName("checkBox")
self.radioButton_3 = QtWidgets.QRadioButton(self.groupBox)
self.radioButton_3.setGeometry(QtCore.QRect(420, 30, 171, 20))
self.radioButton_3.setObjectName("radioButton_3")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(280, 40, 411, 31))
self.lineEdit.setObjectName("lineEdit")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(130, 40, 131, 31))
self.label_3.setObjectName("label_3")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(140, 125, 661, 21))
self.label_5.setText("")
self.label_5.setObjectName("label_5")
self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setGeometry(QtCore.QRect(130, 110, 681, 101))
self.scrollArea.setAccessibleDescription("")
self.scrollArea.setWidgetResizable(False)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 679, 99))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
c=10
n=0
x = 10
y = 10
print(self.sLen)
for i in range(self.sLen):
if(n==4):
n=0
x=10
y=y+20
self.checkbox1 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
self.checkbox1.setGeometry(QtCore.QRect(x+1, y+1, 161, 20))
self.checkbox1.setText(self.serverName[i])
self.checkbox1.setObjectName(self.serverName[i])
print(x)
print(n)
n=n+1
if(n==4):
x=10
y = y+20
else:
x= x+180
## self.checkBox_2 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_2.setGeometry(QtCore.QRect(10, 10, 161, 20))
## self.checkBox_2.setObjectName("checkBox_2")
## self.checkBox_3 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_3.setGeometry(QtCore.QRect(180, 10, 131, 20))
## self.checkBox_3.setObjectName("checkBox_3")
## self.checkBox_4 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_4.setGeometry(QtCore.QRect(350, 10, 151, 20))
## self.checkBox_4.setObjectName("checkBox_4")
## self.checkBox_5 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_5.setGeometry(QtCore.QRect(530, 10, 141, 20))
## self.checkBox_5.setObjectName("checkBox_5")
## self.checkBox_6 = QtWidgets.QCheckBox(self.scrollAreaWidgetContents)
## self.checkBox_6.setGeometry(QtCore.QRect(10, 30, 161, 20))
## self.checkBox_6.setObjectName("checkBox_6")
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.scrollArea_2 = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea_2.setGeometry(QtCore.QRect(140, 380, 681, 191))
self.scrollArea_2.setWidgetResizable(True)
self.scrollArea_2.setObjectName("scrollArea_2")
self.scrollAreaWidgetContents_2 = QtWidgets.QWidget()
self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 679, 189))
self.scrollAreaWidgetContents_2.setObjectName("scrollAreaWidgetContents_2")
self.scrollArea_2.setWidget(self.scrollAreaWidgetContents_2)
self.line = QtWidgets.QFrame(self.centralwidget)
self.line.setGeometry(QtCore.QRect(70, 340, 811, 20))
self.line.setFrameShape(QtWidgets.QFrame.HLine)
self.line.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line.setObjectName("line")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 951, 26))
self.menubar.setObjectName("menubar")
self.menuhelp = QtWidgets.QMenu(self.menubar)
self.menuhelp.setObjectName("menuhelp")
MainWindow.setMenuBar(self.menubar)
self.actionAbout = QtWidgets.QAction(MainWindow)
self.actionAbout.setObjectName("actionAbout")
self.menuhelp.addAction(self.actionAbout)
self.menubar.addAction(self.menuhelp.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label_2.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:10pt;\">Component</span></p></body></html>"))
self.comboBox_2.setItemText(0, _translate("MainWindow", "eComm"))
self.comboBox_2.setItemText(1, _translate("MainWindow", "workproc"))
self.pushButton.setText(_translate("MainWindow", "Search"))
self.groupBox.setTitle(_translate("MainWindow", "Other Details"))
self.radioButton.setText(_translate("MainWindow", "Only Search in Archive"))
self.radioButton_2.setText(_translate("MainWindow", "Also Search in Archive"))
self.checkBox.setText(_translate("MainWindow", "Auto Download"))
self.radioButton_3.setText(_translate("MainWindow", "Only Search in Log"))
self.label_3.setText(_translate("MainWindow", "<html><head/><body><p>Search String<span style=\" color:#ff0000;\">*</span></p></body></html>"))
## self.checkBox_2.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_3.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_4.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_5.setText(_translate("MainWindow", "CheckBox"))
## self.checkBox_6.setText(_translate("MainWindow", "CheckBox"))
self.menuhelp.setTitle(_translate("MainWindow", "Help"))
self.actionAbout.setText(_translate("MainWindow", "About"))
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_())
as you can see the scroll area is not showing properly.i have some 20 servers to show.this server list is coming from ini file and i need to show the check boxes in that scroll area.
you need to use QLayout to arrange you widgets. check out
Qt Designer -> "Widget Box" docker -> "Layouts" section
For the scrollarea part:
create scrollarea scroll_area = QScrollArea()
create view widget you want to put inside scrollarea base_widget = QWidget()
install view layout for view widget base_layout = QVBoxLayout(base_widget)
install view widget into scrollarea scroll_area.setWidget(base_widget)
fill in objects with view layout base_layout.addWidget(QLabel('foo')) base_layout.addWidget(QPushButton('bar')) base_layout.addLayout(my_sub_layout) etc.

PyQt5 identical push buttons, one works one doesn't

I tried writing some codes using pyqt5 and have two main windows and push buttons, for both cases I have done same but one works on clicked.connect and one doesn't. Pasting code for the button which doesn't work. I call this window after closing first one
I tried creating module outside of class and that worked but then struggled with referencing to lineEdit objects
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
app = QtWidgets.QApplication(sys.argv)
PREMIX = QtWidgets.QMainWindow()
logo = QtGui.QIcon()
def init_login():
ui = Ui_PREMIXlogin()
ui.setupUi(PREMIX)
PREMIX.show()
sys.exit(app.exec_())
def close_login():
PREMIX.hide()
def init_main():
ui = Ui_PREMIX()
ui.setupUi(PREMIX)
PREMIX.show()
class Ui_PREMIXlogin(object):
def setupUi(self, PREMIXlogin):
PREMIXlogin.setObjectName("PREMIXlogin")
PREMIXlogin.setEnabled(True)
PREMIXlogin.resize(280, 319)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(
PREMIXlogin.sizePolicy().hasHeightForWidth()
)
PREMIXlogin.setSizePolicy(sizePolicy)
PREMIXlogin.setMinimumSize(QtCore.QSize(280, 319))
PREMIXlogin.setMaximumSize(QtCore.QSize(280, 319))
PREMIXlogin.setMouseTracking(False)
PREMIXlogin.setWindowIcon(logo)
PREMIXlogin.setAutoFillBackground(False)
PREMIXlogin.setStyleSheet("background-color: rgb(255, 255, 255);")
self.centralwidget = QtWidgets.QWidget(PREMIXlogin)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(36, 190, 61, 20))
font = QtGui.QFont()
font.setPointSize(10)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(36, 220, 61, 20))
font = QtGui.QFont()
font.setPointSize(10)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(110, 190, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(110, 220, 113, 20))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setInputMethodHints(
QtCore.Qt.ImhHiddenText
| QtCore.Qt.ImhNoAutoUppercase
| QtCore.Qt.ImhNoPredictiveText
| QtCore.Qt.ImhSensitiveData
)
self.lineEdit_2.setFrame(True)
self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
self.lineEdit_2.setObjectName("lineEdit_2")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(100, 260, 81, 31))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(False)
font.setWeight(50)
self.pushButton.setFont(font)
self.pushButton.setAutoFillBackground(False)
self.pushButton.setStyleSheet("background-color: rgb(222, 31, 38);")
self.pushButton.setObjectName("pushButton")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(30, 20, 231, 151))
self.frame.setStyleSheet(
"image: url(:/images/59301307_2353273704903079_3349139235981492224_n.png);"
)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
PREMIXlogin.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(PREMIXlogin)
self.statusbar.setObjectName("statusbar")
PREMIXlogin.setStatusBar(self.statusbar)
self.retranslateUi(PREMIXlogin)
QtCore.QMetaObject.connectSlotsByName(PREMIXlogin)
def retranslateUi(self, PREMIXlogin):
_translate = QtCore.QCoreApplication.translate
PREMIXlogin.setWindowTitle(_translate("PREMIXlogin", "PREMIX Login"))
self.label.setText(_translate("PREMIXlogin", "Username"))
self.label_2.setText(_translate("PREMIXlogin", "Password"))
self.pushButton.setText(_translate("PREMIXlogin", "Log in"))
self.pushButton.clicked.connect(self.on_click)
def on_click(self):
if True:
close_login()
init_main()
class Ui_PREMIX(object):
def setupUi(self, PREMIX):
PREMIX.setObjectName("PREMIX")
PREMIX.resize(473, 179)
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed
)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(PREMIX.sizePolicy().hasHeightForWidth())
PREMIX.setSizePolicy(sizePolicy)
PREMIX.setMinimumSize(QtCore.QSize(473, 179))
PREMIX.setMaximumSize(QtCore.QSize(473, 179))
PREMIX.setStyleSheet("background-color: rgb(255, 255, 255);")
PREMIX.setWindowIcon(logo)
self.centralwidget = QtWidgets.QWidget(PREMIX)
self.centralwidget.setObjectName("centralwidget")
self.Product = QtWidgets.QLabel(self.centralwidget)
self.Product.setGeometry(QtCore.QRect(30, 20, 61, 20))
font = QtGui.QFont()
font.setPointSize(13)
self.Product.setFont(font)
self.Product.setObjectName("Product")
self.Productname = QtWidgets.QLineEdit(self.centralwidget)
self.Productname.setGeometry(QtCore.QRect(100, 20, 251, 20))
self.Productname.setObjectName("Productname")
self.Checkbutton = QtWidgets.QPushButton(self.centralwidget)
self.Checkbutton.setGeometry(QtCore.QRect(370, 20, 81, 21))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(False)
font.setWeight(50)
self.Checkbutton.setFont(font)
self.Checkbutton.setAutoFillBackground(False)
self.Checkbutton.setStyleSheet("background-color: rgb(222, 31, 38);")
self.Checkbutton.setObjectName("Checkbutton")
self.logo = QtWidgets.QFrame(self.centralwidget)
self.logo.setGeometry(QtCore.QRect(359, 60, 101, 80))
self.logo.setStyleSheet(
"image: url(:/images/59301307_2353273704903079_3349139235981492224_n.png);"
)
self.logo.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.logo.setFrameShadow(QtWidgets.QFrame.Raised)
self.logo.setObjectName("logo")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(180, 80, 91, 31))
self.lineEdit.setMinimumSize(QtCore.QSize(89, 31))
self.lineEdit.setMaximumSize(QtCore.QSize(91, 31))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setReadOnly(True)
PREMIX.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(PREMIX)
self.statusbar.setObjectName("statusbar")
PREMIX.setStatusBar(self.statusbar)
self.retranslateUi(PREMIX)
QtCore.QMetaObject.connectSlotsByName(PREMIX)
def retranslateUi(self, PREMIX):
_translate = QtCore.QCoreApplication.translate
PREMIX.setWindowTitle(_translate("PREMIX", "PREMIX"))
self.Product.setText(_translate("PREMIX", "Product"))
self.Checkbutton.setText(_translate("PREMIX", "Check"))
self.Checkbutton.clicked.connect(self.on_click)
def on_click(self):
print("aaaaaa")
product = self.Productname.text()
print(product)
def main():
init_login()
if __name__ == "__main__":
main()
I want Checkbutton on click event to work, please help
For Ui_Premix, used self.Checkbutton.clicked.connect(Ui_PREMIX.on_click) instead of
self.on_click even though on_click event was inside class, it works now

Style-sheets in PyQt5 bakground-image pushbutton turns it into a no-image pushbutton

I use Qt-Designer to create a mainwindow and used stylesheets to make a button changeimage when pressed. however after running it the background-image and the pressed image does not work.
This is the stylesheet input from Qt-Designer:
QPushButton#House1 {
border-image: url(:/img/Crystal_Clear_app_kfm_home.png);
}
QPushButton#House:pressed {
border-image: url(:/img/Search_in_home.png);
}
This is my mainWindow.py :
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import resources_rc
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(811, 600)
MainWindow.setStyleSheet("background-color: rgb(211, 211, 211);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
###################house 1#######
self.house2 = QtWidgets.QPushButton(self.centralwidget)
self.house1.setGeometry(QtCore.QRect(61, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house1.sizePolicy().hasHeightForWidth())
self.house1.setSizePolicy(sizePolicy)
self.house1.setStyleSheet("#House1 {\n"
"border-image: url(:/img/Crystal_Clear_app_kfm_home.png);\n"
"}\n"
"#House:Pressed {\n"
" border-image: url(:/img/Search_in_home.png);\n"
"}")
self.house1.setText("")
self.house1.setObjectName("house1")
####################end #############
self.house4 = QtWidgets.QPushButton(self.centralwidget)
self.house4.setGeometry(QtCore.QRect(61, 203, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house4.sizePolicy().hasHeightForWidth())
self.house4.setSizePolicy(sizePolicy)
self.house4.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house4.setText("")
self.house4.setObjectName("house4")
self.house2 = QtWidgets.QPushButton(self.centralwidget)
self.house2.setGeometry(QtCore.QRect(293, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house2.sizePolicy().hasHeightForWidth())
self.house2.setSizePolicy(sizePolicy)
self.house2.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house2.setText("")
self.house2.setObjectName("house2")
self.house3 = QtWidgets.QPushButton(self.centralwidget)
self.house3.setGeometry(QtCore.QRect(524, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house3.sizePolicy().hasHeightForWidth())
self.house3.setSizePolicy(sizePolicy)
self.house3.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house3.setText("")
self.house3.setObjectName("house3")
self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_8.setGeometry(QtCore.QRect(293, 384, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_8.sizePolicy().hasHeightForWidth())
self.pushButton_8.setSizePolicy(sizePolicy)
self.pushButton_8.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.pushButton_8.setText("")
self.pushButton_8.setObjectName("pushButton_8")
self.house5 = QtWidgets.QPushButton(self.centralwidget)
self.house5.setGeometry(QtCore.QRect(293, 203, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house5.sizePolicy().hasHeightForWidth())
self.house5.setSizePolicy(sizePolicy)
self.house5.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house5.setText("")
self.house5.setObjectName("house5")
self.house6 = QtWidgets.QPushButton(self.centralwidget)
self.house6.setGeometry(QtCore.QRect(524, 203, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house6.sizePolicy().hasHeightForWidth())
self.house6.setSizePolicy(sizePolicy)
self.house6.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.house6.setText("")
self.house6.setObjectName("house6")
self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_7.setGeometry(QtCore.QRect(61, 384, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_7.sizePolicy().hasHeightForWidth())
self.pushButton_7.setSizePolicy(sizePolicy)
self.pushButton_7.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.pushButton_7.setText("")
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_9 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_9.setGeometry(QtCore.QRect(524, 384, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.pushButton_9.sizePolicy().hasHeightForWidth())
self.pushButton_9.setSizePolicy(sizePolicy)
self.pushButton_9.setStyleSheet("border-image: url(:/img/Crystal_Clear_app_kfm_home.png);")
self.pushButton_9.setText("")
self.pushButton_9.setObjectName("pushButton_9")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 811, 27))
self.menubar.setObjectName("menubar")
self.menuFile = QtWidgets.QMenu(self.menubar)
self.menuFile.setObjectName("menuFile")
self.menuAdmin = QtWidgets.QMenu(self.menubar)
self.menuAdmin.setObjectName("menuAdmin")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionOpen = QtWidgets.QAction(MainWindow)
self.actionOpen.setObjectName("actionOpen")
self.actionSave = QtWidgets.QAction(MainWindow)
self.actionSave.setObjectName("actionSave")
self.actionSave_As = QtWidgets.QAction(MainWindow)
self.actionSave_As.setObjectName("actionSave_As")
self.actionLogin = QtWidgets.QAction(MainWindow)
self.actionLogin.setObjectName("actionLogin")
self.actionSign_Up = QtWidgets.QAction(MainWindow)
self.actionSign_Up.setObjectName("actionSign_Up")
self.menuFile.addAction(self.actionOpen)
self.menuFile.addAction(self.actionSave)
self.menuFile.addAction(self.actionSave_As)
self.menuAdmin.addAction(self.actionLogin)
self.menuAdmin.addAction(self.actionSign_Up)
self.menubar.addAction(self.menuFile.menuAction())
self.menubar.addAction(self.menuAdmin.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "SWTDI Main Window"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
self.menuAdmin.setTitle(_translate("MainWindow", "Admin"))
self.actionOpen.setText(_translate("MainWindow", "Open"))
self.actionSave.setText(_translate("MainWindow", "Save"))
self.actionSave_As.setText(_translate("MainWindow", "Save As"))
self.actionLogin.setText(_translate("MainWindow", "Login"))
self.actionSign_Up.setText(_translate("MainWindow", "Sign Up"))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(w)
w.show()
sys.exit(app.exec())
resource file:
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>img/house.png</file>
<file>img/house2.png</file>
<file>img/NM_State_University_logo.png</file>
<file>img/NMlogo_1colorstate_black.png</file>
<file>img/NMlogo_1colorstate_red.tif</file>
<file>img/Crystal_Clear_app_kfm_home.png</file>
<file>img/Gohomenew.png</file>
<file>img/Search_in_home.png</file>
</qresource>
</RCC>
The button was created by the line I commented out. but anyways
I created another button with similar code I have seen around.
###################house 1#######
self.house1 = QPushButton(self.centralwidget)
#self.house1 = QtWidgets.QPushButton(self.centralwidget)
self.house1.setGeometry(QtCore.QRect(61, 21, 32, 19))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.house1.sizePolicy().hasHeightForWidth())
self.house1.setSizePolicy(sizePolicy)
self.house1.setStyleSheet("#House1 {\n"
"border-image: url(:/img/Crystal_Clear_app_kfm_home.png);\n"
"}\n"
"#House:Pressed {\n"
" border-image: url(:/img/Search_in_home.png);\n"
"}")
self.house1.setText("")
self.house1.setObjectName("house1")
####################end #############
The problem is caused by not using the names correctly, QStyleSheet uses the objectName property, in your case the QPushButton self.house1:
self.house1.setObjectName("house1")
Before writing that line the button has no objectName (actually its objectName is "") so it is not appropriate to place the QStyleSheet by the objectName.
What you should do is place an objectName and then you can just assign the filtered QStyleSheet by its name.
self.house1 = QtWidgets.QPushButton(self.centralwidget)
self.house1.setObjectName("house1")
self.house1.setStyleSheet("#house1 {\n"
"border-image: url(:/img/Crystal_Clear_app_kfm_home.png);\n"
"}\n"
"#house1:Pressed {\n"
" border-image: url(:/img/Search_in_home.png);\n"
"}")
This attribute is case-sensitive so you must place it exactly the same as setObjectName().
For more information you should read the following:
http://doc.qt.io/qt-5/stylesheet-syntax.html
http://doc.qt.io/qt-5/stylesheet-reference.html

Categories

Resources