PyQt5 identical push buttons, one works one doesn't - python

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

Related

AutoResize Existing Main Window pyqt5

i already designed a QMainWindow with QT Designer contain centralWidget which contain
pushbutton
labels
textEdit
after coding all the project I realized that the mainwindow doesn't fit all PC screens
so I need to auto resize all objects to fit when I try layout vertically or horizontally
all object's positions change
enter image description here
enter image description here
class Ui_MainWindow_login(object):
def setupUi(self, MainWindow_login):
MainWindow_login.setObjectName("MainWindow_login")
MainWindow_login.resize(1121, 901)
MainWindow_login.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(MainWindow_login)
self.centralwidget.setObjectName("centralwidget")
self.label_login_title = QtWidgets.QLabel(self.centralwidget)
self.label_login_title.setGeometry(QtCore.QRect(230, 30, 621, 71))
font = QtGui.QFont()
font.setFamily("Traditional Arabic")
font.setPointSize(28)
font.setBold(True)
font.setItalic(False)
font.setWeight(75)
self.label_login_title.setFont(font)
self.label_login_title.setObjectName("label_login_title")
self.label_login_UNAME = QtWidgets.QLabel(self.centralwidget)
self.label_login_UNAME.setGeometry(QtCore.QRect(710, 230, 321, 71))
font = QtGui.QFont()
font.setFamily("Traditional Arabic")
font.setPointSize(28)
font.setBold(True)
font.setItalic(False)
font.setWeight(75)
self.label_login_UNAME.setFont(font)
self.label_login_UNAME.setObjectName("label_login_UNAME")
self.label_PASS = QtWidgets.QLabel(self.centralwidget)
self.label_PASS.setGeometry(QtCore.QRect(650, 400, 211, 71))
font = QtGui.QFont()
font.setFamily("Traditional Arabic")
font.setPointSize(28)
font.setBold(True)
font.setItalic(False)
font.setWeight(75)
font_text = QtGui.QFont()
font_text.setFamily("Traditional Arabic")
font_text.setPointSize(24)
font_text.setBold(True)
font_text.setItalic(False)
font_text.setWeight(75)
self.label_PASS.setFont(font)
self.label_PASS.setObjectName("label_PASS")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(190, 230, 511, 61))
self.textEdit.setObjectName("textEdit")
self.textEdit.setFont(font_text)
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(190, 410, 511, 61))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.setFont(font_text)
self.lineEdit.setEchoMode(QtWidgets.QLineEdit.Password)
self.pushButton_login = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_login.setGeometry(QtCore.QRect(360, 550, 251, 101))
font = QtGui.QFont()
font.setFamily("Traditional Arabic")
font.setPointSize(22)
font.setBold(True)
font.setWeight(75)
self.pushButton_login.setFont(font)
self.pushButton_login.setObjectName("pushButton_login")
self.pushButton_login.clicked.connect(lambda: self.open_next_page())
MainWindow_login.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow_login)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1121, 21))
self.menubar.setObjectName("menubar")
MainWindow_login.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow_login)
self.statusbar.setObjectName("statusbar")
MainWindow_login.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow_login)
QtCore.QMetaObject.connectSlotsByName(MainWindow_login)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow_login = QtWidgets.QMainWindow()
ui = Ui_MainWindow_login()
ui.setupUi(MainWindow_login)
MainWindow_login.show()
sys.exit(app.exec_())

Qt5 QComboBox focusOutEvent

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.

How do I make Average Mark Calculator in PyQT?

I want to use PyQT to calculate average marks from 1 to 5 (5, 4, 4 = 4,33). It shows me error and why I can't do this. What's the problem?
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(412, 239)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(100, 10, 211, 31))
font = QtGui.QFont()
font.setFamily("Montserrat")
font.setPointSize(20)
font.setItalic(True)
self.label.setFont(font)
self.label.setObjectName("label")
self.one = QtWidgets.QPushButton(Dialog)
self.one.setGeometry(QtCore.QRect(20, 150, 61, 41))
font = QtGui.QFont()
font.setFamily("Montserrat SemiBold")
font.setPointSize(14)
font.setBold(True)
font.setUnderline(False)
font.setWeight(75)
self.one.setFont(font)
self.one.setStyleSheet("background-color: rgb(255, 170, 0);")
self.one.setObjectName("one")
self.two = QtWidgets.QPushButton(Dialog)
self.two.setGeometry(QtCore.QRect(100, 150, 61, 41))
font = QtGui.QFont()
font.setFamily("Montserrat SemiBold")
font.setPointSize(14)
font.setUnderline(False)
self.two.setFont(font)
self.two.setStyleSheet("background-color: rgb(255, 170, 0);")
self.two.setObjectName("two")
self.three = QtWidgets.QPushButton(Dialog)
self.three.setGeometry(QtCore.QRect(180, 150, 61, 41))
font = QtGui.QFont()
font.setFamily("Montserrat SemiBold")
font.setPointSize(14)
font.setUnderline(False)
self.three.setFont(font)
self.three.setStyleSheet("background-color: rgb(255, 170, 0);")
self.three.setObjectName("three")
self.four = QtWidgets.QPushButton(Dialog)
self.four.setGeometry(QtCore.QRect(260, 150, 61, 41))
font = QtGui.QFont()
font.setFamily("Montserrat SemiBold")
font.setPointSize(14)
font.setUnderline(False)
self.four.setFont(font)
self.four.setStyleSheet("background-color: rgb(255, 170, 0);")
self.four.setObjectName("four")
self.five = QtWidgets.QPushButton(Dialog)
self.five.setGeometry(QtCore.QRect(340, 150, 61, 41))
font = QtGui.QFont()
font.setFamily("Montserrat SemiBold")
font.setPointSize(14)
font.setBold(True)
font.setUnderline(False)
font.setWeight(75)
self.five.setFont(font)
self.five.setStyleSheet("background-color: rgb(255, 170, 0);")
self.five.setObjectName("five")
self.Marks = QtWidgets.QLabel(Dialog)
self.Marks.setGeometry(QtCore.QRect(10, 50, 391, 31))
font = QtGui.QFont()
font.setFamily("Montserrat Medium")
font.setPointSize(17)
font.setBold(False)
font.setItalic(False)
font.setUnderline(False)
font.setWeight(50)
font.setStrikeOut(False)
font.setKerning(True)
self.Marks.setFont(font)
self.Marks.setText("")
self.Marks.setAlignment(QtCore.Qt.AlignCenter)
self.Marks.setObjectName("Marks")
self.Result = QtWidgets.QLabel(Dialog)
self.Result.setGeometry(QtCore.QRect(140, 100, 131, 31))
font = QtGui.QFont()
font.setFamily("Montserrat Medium")
font.setPointSize(21)
self.Result.setFont(font)
self.Result.setMouseTracking(False)
self.Result.setLayoutDirection(QtCore.Qt.LeftToRight)
self.Result.setText("")
self.Result.setAlignment(QtCore.Qt.AlignCenter)
self.Result.setObjectName("Result")
self.Apply = QtWidgets.QPushButton(Dialog)
self.Apply.setGeometry(QtCore.QRect(130, 200, 75, 23))
font = QtGui.QFont()
font.setFamily("Montserrat")
font.setPointSize(10)
self.Apply.setFont(font)
self.Apply.setObjectName("Apply")
self.Clear = QtWidgets.QPushButton(Dialog)
self.Clear.setGeometry(QtCore.QRect(220, 200, 75, 23))
font = QtGui.QFont()
font.setFamily("Montserrat")
font.setPointSize(10)
self.Clear.setFont(font)
self.Clear.setObjectName("Clear")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.add_functions()
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Средний балл"))
self.one.setText(_translate("Dialog", "1"))
self.two.setText(_translate("Dialog", "2"))
self.three.setText(_translate("Dialog", "3"))
self.four.setText(_translate("Dialog", "4"))
self.five.setText(_translate("Dialog", "5"))
self.Apply.setText(_translate("Dialog", "Принять"))
self.Clear.setText(_translate("Dialog", "Очистить"))
def add_functions(self):
self.one.clicked.connect(lambda: self.write_number(self.one.text()))
self.two.clicked.connect(lambda: self.write_number(self.two.text()))
self.three.clicked.connect(lambda: self.write_number(self.three.text()))
self.four.clicked.connect(lambda: self.write_number(self.four.text()))
self.five.clicked.connect(lambda: self.write_number(self.five.text()))
self.Apply.clicked.connect(self.results)
def write_number(self, number):
marks = []
marks.append(number)
self.Marks.setText(self.Marks.text() + number)
def results(self, number, marks):
res = sum(marks) / len(marks)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
I used tkinter before, so I don't know how to make it, because it shows me error:
"TypeError: Ui_Dialog.write_number() missing 1 required positional argument: 'marks'"
First You have to pass arguments to results function
and marks = [] will reset the marks list every time a button is pressed..
so better make marks global variable by self.marks = [] and append integers to it..
so to append numbers to it use self.marks.append(int(number))
and remove the numbers argument from results as it is not necessary
so results function will look like this..
def results(self, marks):
res = sum(marks) / len(marks)
print(res) #or return as your wish
write_number function should look like this
def write_number(self, number):
self.marks.append(int(number))
self.Marks.setText(self.Marks.text() + number)
and declare the self.marks variable after self.add_functions() statement
self.add_functions()
self.marks = []
and change Apply button connect Function
self.Apply.clicked.connect(lambda: self.results(self.marks))
so add_function should look like this..
def add_functions(self):
self.one.clicked.connect(lambda: self.write_number(self.one.text()))
self.two.clicked.connect(lambda: self.write_number(self.two.text()))
self.three.clicked.connect(lambda: self.write_number(self.three.text()))
self.four.clicked.connect(lambda: self.write_number(self.four.text()))
self.five.clicked.connect(lambda: self.write_number(self.five.text()))
self.Apply.clicked.connect(lambda: self.results(self.marks))

How to pass parameters bw two windows in pyqt5?

I am working on a password manager gui app. Using Qtdesigner I created a main window where I asked for username and password from user. and a dialog window where display the user their username and passwords for different apps and websites. I also called that dialog window from my main window so that when user press login button then if their login credential is correct , the dialog window will appear.
Now what I want is that when dialog window appears,after clicking push button it should show all data of the user which is stored in a table with that username(like if username is alex then a table with name alex is there in db with username and passwords of different apps/websites).
Hope my question is understandable enough to be answered. I can further clarify if anyone interested to answer.
MY mainwindow file------>
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
from main2 import Ui_Dialog
class Ui_MainWindow(object):
def openwindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_Dialog()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(553, 400)
MainWindow.setMinimumSize(QtCore.QSize(553, 400))
MainWindow.setMaximumSize(QtCore.QSize(553, 400))
MainWindow.setAutoFillBackground(False)
MainWindow.setStyleSheet("background-color:\"PowderBlue\"")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(-1, -5, 561, 61))
font = QtGui.QFont()
font.setFamily("Dosis ExtraBold")
font.setPointSize(30)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(87, 8, 97);")
self.label.setFrameShape(QtWidgets.QFrame.NoFrame)
self.label.setFrameShadow(QtWidgets.QFrame.Plain)
self.label.setLineWidth(7)
self.label.setObjectName("label")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(28, 120, 511, 261))
self.frame.setStyleSheet("background-color: rgb(176, 224, 230);\n"
"")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.username = QtWidgets.QLineEdit(self.frame)
self.username.setGeometry(QtCore.QRect(224, 0, 261, 41))
font = QtGui.QFont()
font.setFamily("Segoe Print")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
self.username.setFont(font)
self.username.setStyleSheet("border :2px solid purple ;\n"
"background-color:PowderBlue;\n"
"border-radius:5px;\n"
"\n"
"\n"
"\n"
"")
self.username.setText("")
self.username.setEchoMode(QtWidgets.QLineEdit.Normal)
self.username.setPlaceholderText("master user")
self.username.setClearButtonEnabled(False)
self.username.setObjectName("username")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(442, 7, 41, 31))
self.label_2.setStyleSheet("image: url(:/user.png);\n"
"background-color:PowderBlue")
self.label_2.setText("")
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.frame)
self.label_3.setGeometry(QtCore.QRect(450, 69, 31, 31))
self.label_3.setAutoFillBackground(False)
self.label_3.setStyleSheet("image: url(:/key.png);\n"
"background-color:PowderBlue")
self.label_3.setText("")
self.label_3.setObjectName("label_3")
self.password = QtWidgets.QLineEdit(self.frame)
self.password.setGeometry(QtCore.QRect(225, 65, 261, 41))
font = QtGui.QFont()
font.setFamily("Segoe Print")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
self.password.setFont(font)
self.password.setPlaceholderText("master password")
self.password.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
self.password.setStyleSheet("border :2px solid purple ;\n"
"border-radius:5px;")
self.password.setText("")
self.password.setEchoMode(QtWidgets.QLineEdit.Password)
self.password.setPlaceholderText("")
self.password.setClearButtonEnabled(True)
self.password.setObjectName("password")
self.login_button = QtWidgets.QPushButton(self.frame,clicked=lambda:self.log_in())
self.login_button.setGeometry(QtCore.QRect(280, 130, 41, 31))
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.login_button.setFont(font)
self.login_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.login_button.setAcceptDrops(True)
self.login_button.setAutoFillBackground(False)
self.login_button.setStyleSheet("image: url(:/login.png);")
self.login_button.setText("")
self.login_button.setFlat(True)
self.login_button.setObjectName("login_button")
self.signup_button = QtWidgets.QPushButton(self.frame,clicked=lambda:self.sign_up())
self.signup_button.setGeometry(QtCore.QRect(388, 130, 41, 31))
self.signup_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.signup_button.setMouseTracking(False)
self.signup_button.setAcceptDrops(True)
self.signup_button.setAutoFillBackground(False)
self.signup_button.setStyleSheet("image: url(:/user (2).png);")
self.signup_button.setText("")
self.signup_button.setFlat(True)
self.signup_button.setObjectName("signup_button")
self.label_4 = QtWidgets.QLabel(self.frame)
self.label_4.setGeometry(QtCore.QRect(25, 7, 141, 141))
self.label_4.setStyleSheet("image: url(:/team.png);")
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.frame)
self.label_5.setGeometry(QtCore.QRect(17, 135, 171, 41))
font = QtGui.QFont()
font.setFamily("Forte")
font.setPointSize(22)
font.setBold(False)
font.setWeight(50)
self.label_5.setFont(font)
self.label_5.setStyleSheet("color: rgb(5, 22, 127);")
self.label_5.setFrameShape(QtWidgets.QFrame.NoFrame)
self.label_5.setFrameShadow(QtWidgets.QFrame.Plain)
self.label_5.setLineWidth(7)
self.label_5.setObjectName("label_5")
self.error_lbl = QtWidgets.QLabel(self.frame)
self.error_lbl.setGeometry(QtCore.QRect(230,165,251,50))
#self.error_lbl.textFormat.RichText()
font = QtGui.QFont()
font.setFamily("Roboto Black")
font.setPointSize(11)
font.setBold(True)
font.setWeight(65)
self.error_lbl.setFont(font)
self.error_lbl.setStyleSheet("color: rgb(255,0,0);")
self.error_lbl.setFrameShape(QtWidgets.QFrame.NoFrame)
self.error_lbl.setFrameShadow(QtWidgets.QFrame.Plain)
self.error_lbl.setLineWidth(1)
self.error_lbl.setObjectName("error_lbl")
self.line_2 = QtWidgets.QFrame(self.frame)
self.line_2.setGeometry(QtCore.QRect(-17, 190, 20, 61))
self.line_2.setFrameShape(QtWidgets.QFrame.VLine)
self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_2.setObjectName("line_2")
self.line_5 = QtWidgets.QFrame(self.frame)
self.line_5.setGeometry(QtCore.QRect(520, -20, 20, 221))
self.line_5.setFrameShape(QtWidgets.QFrame.VLine)
self.line_5.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_5.setObjectName("line_5")
self.label_6 = QtWidgets.QLabel(self.frame)
self.label_6.setGeometry(QtCore.QRect(349, 231, 41, 31))
self.label_6.setStyleSheet("image: url(:/copyright.png);")
self.label_6.setText("")
self.label_6.setObjectName("label_6")
self.label_7 = QtWidgets.QLabel(self.frame)
self.label_7.setGeometry(QtCore.QRect(380, 234, 111, 20))
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_7.setFont(font)
self.label_7.setObjectName("label_7")
self.password.raise_()
self.username.raise_()
self.label_2.raise_()
self.label_3.raise_()
self.login_button.raise_()
self.signup_button.raise_()
self.label_4.raise_()
self.label_5.raise_()
self.error_lbl.raise_()
self.line_2.raise_()
self.line_5.raise_()
self.label_6.raise_()
self.label_7.raise_()
self.info_button = QtWidgets.QPushButton(self.centralwidget)
self.info_button.setGeometry(QtCore.QRect(510, 0, 41, 23))
self.info_button.setStyleSheet("image: url(:/information-button.png);")
self.info_button.setText("")
self.info_button.setFlat(True)
self.info_button.setObjectName("info_button")
self.line_3 = QtWidgets.QFrame(self.centralwidget)
self.line_3.setGeometry(QtCore.QRect(9, 101, 20, 271))
self.line_3.setFrameShape(QtWidgets.QFrame.VLine)
self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_3.setObjectName("line_3")
self.line_6 = QtWidgets.QFrame(self.centralwidget)
self.line_6.setGeometry(QtCore.QRect(520, 101, 21, 271))
self.line_6.setFrameShape(QtWidgets.QFrame.VLine)
self.line_6.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_6.setObjectName("line_6")
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(334, 0, 81, 61))
self.label_8.setStyleSheet("image: url(:/password.png);")
self.label_8.setText("")
self.label_8.setObjectName("label_8")
self.line_8 = QtWidgets.QFrame(self.centralwidget)
self.line_8.setGeometry(QtCore.QRect(19, 90, 511, 20))
self.line_8.setFrameShape(QtWidgets.QFrame.HLine)
self.line_8.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_8.setObjectName("line_8")
self.line_4 = QtWidgets.QFrame(self.centralwidget)
self.line_4.setGeometry(QtCore.QRect(19, 364, 361, 16))
self.line_4.setFrameShape(QtWidgets.QFrame.HLine)
self.line_4.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_4.setObjectName("line_4")
self.line_7 = QtWidgets.QFrame(self.centralwidget)
self.line_7.setGeometry(QtCore.QRect(513, 364, 16, 16))
self.line_7.setFrameShape(QtWidgets.QFrame.HLine)
self.line_7.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_7.setObjectName("line_7")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 553, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def log_in(self):
conn = sqlite3.connect("master.db")
cur = conn.cursor()
user = self.username.text()
password = self.password.text()
print(user)
print(password)
if user=="" or password=="" :
self.error_lbl.setText( "materuser & masterpassword\nrequired !")
else:
cur.execute("SELECT rowid , *FROM master_database WHERE master_users = ?", (user,))
c=cur.fetchone()
conn.commit()
conn.close()
if c==None:
self.error_lbl.setText( f"No data for {user}\nKindly register for new user !" )
else:
if c[2] == password:
#self.error_lbl.setText("Successfully verified.\nClick to Login")
self.window = QtWidgets.QMainWindow()
self.ui = Ui_Dialog()
self.ui.setupUi(self.window)
self.window.show()
else:
self.error_lbl.setText("Incorrect Password !" )
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_())
My Dialog window file------->'
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main2.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
class Ui_Dialog(object):
def loaddata(self):
connection = sqlite3.connect('master.db')
query = "SELECT * FROM a"
c = connection.execute(query)
self.tableWidget.setRowCount(0)
for row_number , row_data in enumerate(c):
self.tableWidget.insertRow(row_number)
for column_number ,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtWidgets.QTableWidgetItem(str(data)))
connection.commit()
connection.close()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(553, 424)
font = QtGui.QFont()
font.setFamily("Consolas")
Dialog.setFont(font)
Dialog.setStyleSheet("background-color: PowderBlue;bn ")
self.label_4 = QtWidgets.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(36, 112, 151, 31))
font = QtGui.QFont()
font.setFamily("Raleway Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(Dialog)
self.label_5.setGeometry(QtCore.QRect(225, 112, 111, 31))
font = QtGui.QFont()
font.setFamily("Raleway Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(Dialog)
self.label_6.setGeometry(QtCore.QRect(396, 112, 121, 31))
font = QtGui.QFont()
font.setFamily("Raleway Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.tableWidget = QtWidgets.QTableWidget(Dialog)
self.tableWidget.setGeometry(QtCore.QRect(10, 150, 531, 271))
self.tableWidget.setMinimumSize(QtCore.QSize(531, 271))
self.tableWidget.setMaximumSize(QtCore.QSize(531, 271))
font = QtGui.QFont()
font.setFamily("Source Sans Pro SemiBold")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.tableWidget.setFont(font)
self.tableWidget.setStyleSheet("color: rgb(73, 6, 127);")
self.tableWidget.setFrameShape(QtWidgets.QFrame.Box)
self.tableWidget.setGridStyle(QtCore.Qt.NoPen)
self.tableWidget.setRowCount(10)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.horizontalHeader().setDefaultSectionSize(177)
self.pushButton = QtWidgets.QPushButton(Dialog,clicked=lambda:self.loaddata())
self.pushButton.setGeometry(QtCore.QRect(200, 30, 75, 23))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label_4.setText(_translate("Dialog", "APP/WEBSITE"))
self.label_5.setText(_translate("Dialog", "USERNAME"))
self.label_6.setText(_translate("Dialog", "PASSWORD"))
self.pushButton.setText(_translate("Dialog", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
I use the QStackedWidget to switch between pages.
app = QApplication(sys.argv)
stackedwidget = QtWidgets.QStackedWidget()
page1 = PageOneClass()
page2 = PageTwoClass()
stackedwidget.addWidget(page1)
stackedwidget.addWidget(page2)
stackedwidget.show()
app.exec_()
This will let you switch between pages without running another file. Now to get values from one page to another, use inheritance.
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.user = self.username.text()
self password = self.password.text() #Now the user, password can be accessed using the class name. dont forget to add self.
class DialogWindow(MainWindow):
def __init__(self, MainWindow):
super(DialogWindow, self).__init__()
usernamefrompage1 = MainWindow.user
passwordfrompage1 = MainWindow.password
app = QApplication(sys.argv)
stackedwidget = QtWidgets.QStackedWidget()
mainwindow_instance = MainWindow()
dialogwindow_instance = DialogWindow(mainwindow_instance)
#we are passing the mainwindow instance to the dialog window instance, this will let us use all the methods and attributes of the mainwindow.
stackedwidget.addWidget(mainwindow_instance)
stackedwidget.addWidget(dialogwindow_instance)
stackedwidget.setCurrentWidget(mainwindow_instance)
stackedwidget.show()
app.exec_()

Implementing Timer to blink Q Push Buttons

Im trying to get some push buttons to blink from a QtDesigner file, but im having trouble using the timer function to change the background of the button.
I was already helped by someone here and their code works great, but I cant seem to integrate the code to what I already have.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1309, 813)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 30, 251, 41))
font = QtGui.QFont()
font.setPointSize(36)
self.label.setFont(font)
self.label.setObjectName("label")
self.voltage_percent = QtWidgets.QLCDNumber(self.centralwidget)
self.voltage_percent.setGeometry(QtCore.QRect(80, 170, 141, 91))
self.voltage_percent.setSmallDecimalPoint(True)
self.voltage_percent.setDigitCount(1)
self.voltage_percent.setProperty("intValue", 5)
self.voltage_percent.setObjectName("voltage_percent")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(1070, 30, 111, 51))
font = QtGui.QFont()
font.setPointSize(36)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.modeTable = QtWidgets.QTableView(self.centralwidget)
self.modeTable.setGeometry(QtCore.QRect(970, 90, 321, 411))
self.modeTable.setObjectName("modeTable")
self.label_12 = QtWidgets.QLabel(self.centralwidget)
self.label_12.setGeometry(QtCore.QRect(170, 190, 51, 61))
font = QtGui.QFont()
font.setPointSize(48)
self.label_12.setFont(font)
self.label_12.setObjectName("label_12")
self.label_13 = QtWidgets.QLabel(self.centralwidget)
self.label_13.setGeometry(QtCore.QRect(580, 30, 91, 41))
font = QtGui.QFont()
font.setPointSize(36)
self.label_13.setFont(font)
self.label_13.setObjectName("label_13")
self.flags_table = QtWidgets.QGraphicsView(self.centralwidget)
self.flags_table.setGeometry(QtCore.QRect(370, 90, 541, 401))
self.flags_table.setObjectName("flags_table")
self.powerup_button = QtWidgets.QPushButton(self.centralwidget)
self.powerup_button.setGeometry(QtCore.QRect(380, 110, 111, 81))
self.powerup_button.setText("")
self.powerup_button.setObjectName("powerup_button")
self.flag = True
timer = QtCore.QTimer(self, interval=1000)
timer.timeout.connect(self.update)
timer.start(1000)
def update_color (self):
if self.flag==True:
self.powerup_button.setStyleSheet("background-color:none;")
else:
self.powerup_button.setStyleSheet("background-color: green;")
self.flag = False
I only included the part where I created my push button for simplicity, there are many more but I figured the same solution applies to all of them since they're the same. Am I just applying the timer wrong? the gui pops up, but the button I want to blink wont blink when I run the program (the powerup button)
Try it:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1309, 813)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(50, 30, 251, 41))
font = QtGui.QFont()
font.setPointSize(36)
self.label.setFont(font)
self.label.setObjectName("label")
self.voltage_percent = QtWidgets.QLCDNumber(self.centralwidget)
self.voltage_percent.setGeometry(QtCore.QRect(80, 170, 141, 91))
self.voltage_percent.setSmallDecimalPoint(True)
self.voltage_percent.setDigitCount(1)
self.voltage_percent.setProperty("intValue", 5)
self.voltage_percent.setObjectName("voltage_percent")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(1070, 30, 111, 51))
font = QtGui.QFont()
font.setPointSize(36)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.modeTable = QtWidgets.QTableView(self.centralwidget)
self.modeTable.setGeometry(QtCore.QRect(970, 90, 321, 411))
self.modeTable.setObjectName("modeTable")
self.label_12 = QtWidgets.QLabel(self.centralwidget)
self.label_12.setGeometry(QtCore.QRect(170, 190, 51, 61))
font = QtGui.QFont()
font.setPointSize(48)
self.label_12.setFont(font)
self.label_12.setObjectName("label_12")
self.label_13 = QtWidgets.QLabel(self.centralwidget)
self.label_13.setGeometry(QtCore.QRect(580, 30, 91, 41))
font = QtGui.QFont()
font.setPointSize(36)
self.label_13.setFont(font)
self.label_13.setObjectName("label_13")
self.flags_table = QtWidgets.QGraphicsView(self.centralwidget)
self.flags_table.setGeometry(QtCore.QRect(370, 90, 541, 401))
self.flags_table.setObjectName("flags_table")
self.powerup_button = QtWidgets.QPushButton(self.centralwidget)
self.powerup_button.setGeometry(QtCore.QRect(380, 110, 111, 81))
self.powerup_button.setText(" ")
self.powerup_button.setObjectName("powerup_button")
self.flag = True
timer = QtCore.QTimer(MainWindow) # +++
timer.timeout.connect(self.update_color)
timer.start(1000)
MainWindow.setCentralWidget(self.centralwidget)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def update_color (self):
if self.flag: #==True:
self.powerup_button.setStyleSheet("#powerup_button {background-color:none;}")
else:
self.powerup_button.setStyleSheet("#powerup_button {background-color: green;}")
# self.flag = False
self.flag = not self.flag # +
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Windows')
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

Categories

Resources