turning Sqlite records into python variables - python

This is my code. The code in email_compare() is my problem. I want it to take the input from the email box and compare it to the emails in my Sqlite database using a linear search. The problem is that though the input is exactly the same it won't output that it is correct. I have had to delete certain parts of my code due to having more code than writing so if it is required I will right back to say what I have deleted. Also I am not sure if you can turn Sqlite records into variables that can actually be compared so if there is another way of doing this then I would greatly appreciate being told how to do it in that method instead of what I am trying to do now.
from PyQt6 import QtCore, QtGui, QtWidgets
import sqlite3
from SIGNUP import Ui_MainWindow
class Ui_Dialog(object):
def openWindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.window)
Dialog.close()
self.window.show()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(480, 620)
Dialog.setStyleSheet("background: rgb(64, 64, 64)")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(210, 60, 200, 50))
self.label.setStyleSheet("color:rgb(255, 255, 255);\n"
"font-size: 28pt;")
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(60, 150, 91, 41))
self.label_2.setStyleSheet("font: 23pt; color: rgb(255, 0, 0)")
self.label_2.setObjectName("label_2")
self.email = QtWidgets.QLineEdit(Dialog)
self.email.setGeometry(QtCore.QRect(60, 190, 251, 41))
self.email.setStyleSheet("color: rgb(255, 255, 255)")
self.email.setObjectName("email")
self.label_3 = QtWidgets.QLabel(Dialog)
self.label_3.setGeometry(QtCore.QRect(60, 260, 101, 41))
self.label_3.setStyleSheet("font: 23pt; color: rgb(255, 0, 0)")
self.label_3.setObjectName("label_3")
self.password = QtWidgets.QLineEdit(Dialog)
self.password.setGeometry(QtCore.QRect(60, 300, 251, 41))
self.password.setStyleSheet("color: rgb(255, 255, 255)")
self.password.setText("")
self.password.setObjectName("password")
self.loginbutton = QtWidgets.QPushButton(Dialog)
self.loginbutton.setGeometry(QtCore.QRect(330, 390, 100, 32))
self.loginbutton.setStyleSheet("color: rgb(255, 0, 0)")
self.loginbutton.setObjectName("loginbutton")
self.loginbutton_2 = QtWidgets.QPushButton(Dialog)
self.loginbutton_2.setGeometry(QtCore.QRect(50, 390, 100, 32))
self.loginbutton_2.setStyleSheet("background-color: rgb(0, 255, 212);")
self.loginbutton_2.setObjectName("loginbutton_2")
self.loginbutton_2.clicked.connect(self.openWindow)
self.loginbutton.clicked.connect(self.show_password)
self.loginbutton.clicked.connect(self.email_compare)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Login"))
self.label_2.setText(_translate("Dialog", "Email"))
self.label_3.setText(_translate("Dialog", "Password"))
self.loginbutton.setText(_translate("Dialog", "Login!"))
self.loginbutton_2.setText(_translate("Dialog", "Sign up!"))
# this is my problem
def email_compare(self):
count = 0
check = self.email.text()
check = "('"+check+"',)"
conn = sqlite3.connect("U+P.db")
c = conn.cursor()
c.execute("SELECT email FROM Details")
compare = c.fetchall()
print(compare)
print(check)
right = False
while count < len(compare) and right == False:
print(compare[count])
if compare[count] == check:
print("Correct email")
right = True
else:
count = count + 1
conn.close()
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())

Related

How to output to the application itself

I wrote a program that adds two numbers entered by the user into QLineEdit.
The result of the addition is output to the console in Pycharm.
How to make the result output to the application itself? (so that the user can see the answer).
I guess that there is some special method for this, but what is it?
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Probnei(object):
def setupUi(self, Probnei):
Probnei.setObjectName("Probnei")
Probnei.resize(439, 309)
self.centralwidget = QtWidgets.QWidget(Probnei)
self.centralwidget.setObjectName("centralwidget")
self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
self.layoutWidget.setGeometry(QtCore.QRect(90, 140, 214, 22))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label_2 = QtWidgets.QLabel(self.layoutWidget)
self.label_2.setObjectName("label_2")
self.horizontalLayout_2.addWidget(self.label_2)
self.Second_n = QtWidgets.QLineEdit(self.layoutWidget)
self.Second_n.setObjectName("Second_n")
self.horizontalLayout_2.addWidget(self.Second_n)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(360, 210, 75, 23))
self.pushButton.setObjectName("pushButton")
self.widget = QtWidgets.QWidget(self.centralwidget)
self.widget.setGeometry(QtCore.QRect(90, 60, 214, 22))
self.widget.setObjectName("widget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.widget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.widget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.First_n = QtWidgets.QLineEdit(self.widget)
self.First_n.setObjectName("First_n")
self.horizontalLayout.addWidget(self.First_n)
Probnei.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(Probnei)
self.menubar.setGeometry(QtCore.QRect(0, 0, 439, 21))
self.menubar.setObjectName("menubar")
Probnei.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(Probnei)
self.statusbar.setObjectName("statusbar")
Probnei.setStatusBar(self.statusbar)
self.retranslateUi(Probnei)
QtCore.QMetaObject.connectSlotsByName(Probnei)
def retranslateUi(self, Probnei):
_translate = QtCore.QCoreApplication.translate
Probnei.setWindowTitle(_translate("Probnei", "MainWindow"))
self.label_2.setText(_translate("Probnei", "Второе число:"))
self.pushButton.setText(_translate("Probnei", "Посчитать"))
self.label.setText(_translate("Probnei", "Первое число:"))
self.pushButton.clicked.connect(self.cal)
def cal(self, Probnei):
val_1 = self.First_n.text()
val_2 = self.Second_n.text()
res = int(val_1) + int(val_2)
print(res)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Probnei = QtWidgets.QMainWindow()
ui = Ui_Probnei()
ui.setupUi(Probnei)
Probnei.show()
sys.exit(app.exec_())
How to make the result output to the application itself?
It's not magic. You just have to create a widget that will display the answer. Then set the text on that widget when you do the calculation.
self.answerLabel = QtWidgets.QLabel(self.widget)
def cal(self, Probnei):
val_1 = self.First_n.text()
val_2 = self.Second_n.text()
res = int(val_1) + int(val_2)
self.answerLabel.setText(res)

How to stop a function without closing the application?

So, I want to close the function on_click_on_start which has while loop in it. And, dont want to close the window after hitting the stop button. Right now, I was using sys.exit() which crashed the program for some reason but, when I used only quit() it was closing the entire app. I am doing this on pyQt5. I am noob to GUI so please help me out. This is extra text which I have added so that I can post the question. Please ignore this.
import res1
import res
from PyQt5 import QtCore, QtGui, QtWidgets
import os
import pyaudio
import numpy as np
import pyautogui
import time
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(800, 600)
MainWindow.setMaximumSize(QtCore.QSize(800, 600))
font = QtGui.QFont()
font.setFamily("Californian FB")
font.setPointSize(14)
MainWindow.setFont(font)
MainWindow.setMouseTracking(True)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(
"../../../../Pictures/ugo_trans.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
MainWindow.setWindowIcon(icon)
MainWindow.setAutoFillBackground(True)
MainWindow.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setAutoFillBackground(False)
self.centralwidget.setStyleSheet("background-color: rgb(255, 0, 0);")
self.centralwidget.setObjectName("centralwidget")
self.Backgroundimage = QtWidgets.QLabel(self.centralwidget)
self.Backgroundimage.setEnabled(True)
self.Backgroundimage.setGeometry(QtCore.QRect(9, 9, 782, 582))
sizePolicy = QtWidgets.QSizePolicy(
QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(
self.Backgroundimage.sizePolicy().hasHeightForWidth())
self.Backgroundimage.setSizePolicy(sizePolicy)
self.Backgroundimage.setMaximumSize(QtCore.QSize(800, 600))
self.Backgroundimage.setStyleSheet("")
self.Backgroundimage.setText("")
self.Backgroundimage.setPixmap(
QtGui.QPixmap(":/newPrefix/Untitled-1.png"))
self.Backgroundimage.setScaledContents(True)
self.Backgroundimage.setObjectName("Backgroundimage")
self.Start = QtWidgets.QPushButton(self.centralwidget)
self.Start.setGeometry(QtCore.QRect(60, 240, 221, 111))
font = QtGui.QFont()
font.setPointSize(16)
self.Start.setFont(font)
self.Start.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 255, 255);")
self.Start.setObjectName("Start")
self.Stop = QtWidgets.QPushButton(self.centralwidget)
self.Stop.setGeometry(QtCore.QRect(510, 240, 221, 111))
font = QtGui.QFont()
font.setPointSize(16)
self.Stop.setFont(font)
self.Stop.setStyleSheet("background-color: rgb(0, 0, 0);\n"
"color: rgb(255, 255, 255);")
self.Stop.setObjectName("Stop")
self.Title = QtWidgets.QLabel(self.centralwidget)
self.Title.setGeometry(QtCore.QRect(210, 70, 361, 61))
font = QtGui.QFont()
font.setPointSize(48)
self.Title.setFont(font)
self.Title.setStyleSheet("color: rgb(255, 255, 255);\n"
"background-color: rgb(0, 0, 0, 60);")
self.Title.setObjectName("Title")
MainWindow.setCentralWidget(self.centralwidget)
self.Start.clicked.connect(self.on_click_on_start) # button pressed on start
self.Stop.clicked.connect(self.on_click_on_stop) # button pressed on stop and it only stops the on_click_on_start
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def on_click_on_start(self):
CHUNK = 2**11
RATE = 48016
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True,
frames_per_buffer=CHUNK)
start = time.time()
while 1:
data = np.fromstring(stream.read(CHUNK), dtype=np.int16)
peak = np.average(np.abs(data))*2
bars = 100*peak/2**16
if bars > 1:
pyautogui.keyDown("NUM0")
start = time.time()
print('down')
else:
pyautogui.keyUp("NUM0")
print('up')
if time.time() - start > 5:
stream.stop_stream()
stream.close()
p.terminate()
break
def on_click_on_stop(self):
# Help me with this function
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Talk to Mute"))
self.Start.setText(_translate("MainWindow", "Start"))
self.Stop.setText(_translate("MainWindow", "Stop"))
self.Title.setText(_translate("MainWindow", "Talk to Mute"))
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_())
Generally using variables is easiest way to manage status. You can use variable for update running status.
running_status = false
Use like
self.running_status = true
in on_click_on_start.
Use
self.running_status = false
in on_click_on_stop. So now you can call your function if running_status is true.

Going back from new window to main window

I have 2 windows, MainScreen and AddFW.
Whenever I go from MainScreen -> AddFW, it works (the MainScreen hides and the AddFW appears).
But when I try going back from AddFW to MainScreen, the window crashes and I get an error: "NameError: name 'AddFW' is not defined".
What am I doing wrong?
MainScreen.py
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLineEdit, QPushButton
from PyQt5 import *
from PyQt5 import QtGui
from AddFW import *
import pyodbc
server = '**'
database = '**'
username = '**'
password = '**'
conn = pyodbc.connect('DRIVER={**};'
'SERVER=' + server + ';'
'DATABASE=' + database + ';'
'UID=' + username + ';'
'PWD=' + password)
class Ui_MainWindow(object):
def OpenAddFW(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_AddFW()
self.ui.setupUi(self.window)
self.window.show()
MainWindow.hide()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1000, 600)
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 0, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
MainWindow.setPalette(palette)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(410, 40, 201, 41))
font = QtGui.QFont()
font.setFamily("Imprint MT Shadow")
font.setPointSize(27)
font.setBold(False)
font.setUnderline(True)
font.setWeight(50)
self.label.setFont(font)
self.label.setObjectName("label")
self.InsertResultsBtn = QtWidgets.QPushButton(self.centralwidget)
self.InsertResultsBtn.setGeometry(QtCore.QRect(58, 226, 160, 130))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.InsertResultsBtn.setFont(font)
self.InsertResultsBtn.setObjectName("InsertResultsBtn")
self.SearchResultsBtn = QtWidgets.QPushButton(self.centralwidget)
self.SearchResultsBtn.setGeometry(QtCore.QRect(300, 226, 160, 130))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.SearchResultsBtn.setFont(font)
self.SearchResultsBtn.setObjectName("SearchResultsBtn")
self.CompareResultsBtn = QtWidgets.QPushButton(self.centralwidget)
self.CompareResultsBtn.setGeometry(QtCore.QRect(541, 226, 160, 130))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.CompareResultsBtn.setFont(font)
self.CompareResultsBtn.setObjectName("CompareResultsBtn")
self.AddFWBtn = QtWidgets.QPushButton(self.centralwidget)
self.AddFWBtn.setGeometry(QtCore.QRect(783, 226, 160, 130))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.AddFWBtn.setFont(font)
self.AddFWBtn.setObjectName("AddFWBtn")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setEnabled(True)
self.label_2.setGeometry(QtCore.QRect(420, 540, 181, 16))
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 0, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 0, 127))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Text, brush)
brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, brush)
self.label_2.setPalette(palette)
self.label_2.setObjectName("label_2")
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", "TestTrack"))
self.InsertResultsBtn.setText(_translate("MainWindow", "Insert Results"))
self.SearchResultsBtn.setText(_translate("MainWindow", "Search Results"))
self.CompareResultsBtn.setText(_translate("MainWindow", "Compare Results"))
self.AddFWBtn.setText(_translate("MainWindow", "Add Firmware"))
self.label_2.setText(_translate("MainWindow", "George Hanna & Nadav Benun"))
self.AddFWBtn.clicked.connect(self.OpenAddFW)
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_())
AddFW
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLineEdit
from PyQt5 import *
from PyQt5 import QtGui
from MainScreen import *
import pyodbc
server = '**'
database = '**'
username = '**'
password = '**'
conn = pyodbc.connect('DRIVER={**};'
'SERVER=' + server + ';'
'DATABASE=' + database + ';'
'UID=' + username + ';'
'PWD=' + password)
def create(conn2, AddToDataBase):
cursor = conn2.cursor()
cursor.execute('insert into dbo.Firmware(Firmware) values(?);', AddToDataBase)
conn2.commit()
print('Added!')
class Ui_AddFW(object):
def AddFWFunc(self):
create(conn, self.FirmwareInsert.text())
def BackToMain(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.window)
self.window.show()
AddFW.hide()
def setupUi(self, AddFW):
AddFW.setObjectName("AddFW")
AddFW.resize(1000, 600)
self.centralwidget = QtWidgets.QWidget(AddFW)
self.centralwidget.setObjectName("centralwidget")
self.AddFWButton = QtWidgets.QPushButton(self.centralwidget)
self.AddFWButton.setGeometry(QtCore.QRect(470, 260, 121, 28))
self.AddFWButton.setObjectName("AddFWButton")
self.Back = QtWidgets.QPushButton(self.centralwidget)
self.Back.setGeometry(QtCore.QRect(40, 40, 51, 28))
self.Back.setObjectName("Back")
self.FirmwareLabel = QtWidgets.QLabel(self.centralwidget)
self.FirmwareLabel.setGeometry(QtCore.QRect(320, 170, 60, 16))
self.FirmwareLabel.setObjectName("FirmwareLabel")
self.Title = QtWidgets.QLabel(self.centralwidget)
self.Title.setGeometry(QtCore.QRect(370, 50, 321, 16))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.Title.setFont(font)
self.Title.setObjectName("Title")
self.FirmwareInsert = QtWidgets.QLineEdit(self.centralwidget)
self.FirmwareInsert.setGeometry(QtCore.QRect(400, 160, 281, 31))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.FirmwareInsert.sizePolicy().hasHeightForWidth())
self.FirmwareInsert.setSizePolicy(sizePolicy)
self.FirmwareInsert.setObjectName("FirmwareInsert")
AddFW.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(AddFW)
self.statusbar.setObjectName("statusbar")
AddFW.setStatusBar(self.statusbar)
self.retranslateUi(AddFW)
QtCore.QMetaObject.connectSlotsByName(AddFW)
def retranslateUi(self, AddFW):
_translate = QtCore.QCoreApplication.translate
AddFW.setWindowTitle(_translate("AddFW", "MainWindow"))
self.AddFWButton.setText(_translate("AddFW", "Insert to DataBase"))
self.Back.setText(_translate("AddFW", "Back"))
self.FirmwareLabel.setText(_translate("AddFW", "Firmware:"))
self.Title.setText(_translate("AddFW", "Add Firmware to the DataBase"))
self.AddFWButton.clicked.connect(self.AddFWFunc)
self.Back.clicked.connect(self.BackToMain)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
AddFW = QtWidgets.QMainWindow()
ui = Ui_AddFW()
ui.setupUi(AddFW)
AddFW.show()
sys.exit(app.exec_())
This happens because Add_FW exists only within the setupUi and retranslateUi methods. It works in MainWindow because you are declaring it below in
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow() <- HERE
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
One quick fix is to pass the AddFW object through connect this way :
self.Back.clicked.connect(lambda: self.BackToMain(AddFW))
But a better way to deal with this would be to inherit QMainWindow in both Ui_AddFW and Ui_MainWindow class due to which they both become an instance of QMainWindow and you wont have to pass the QMainWindow when you are instantiating it.
Check this out: PyQt class inheritance
Finally,
Instead of showing and hiding screens within each object, I would suggest creating a main.py and handle the show/hide logic there this way:
main.py
from AddFW import Ui_AddFW
from MainScreen import Ui_MainWindow
def handle_back_to_main():
add_fw.hide()
main_screen.show()
def handle_back_to_add_fw():
main_screen.hide()
add_fw.show()
add_fw = Ui_AddFW()
main_screen = Ui_MainWindow()
add_fw.Back.clicked.connect(handle_back_to_main)
main_screen.AddFWBtn.clicked.connect(handle_back_to_add_fw)

PyQt5 QPushbutton to store ariables Python

Using PyQt5 QPlaintext to store variables. Example AO = userinput?
What I have so far minus the GUI setup. I need to store it as simple variables so i can output to a DOCX. I know I have to manually user input for every QPlaintextbox available. would I use the getText(), link it to the push button to generate the variables. Thank you for your time.
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(1340, 922)
self.OPORD = QtWidgets.QTabWidget(Dialog)
self.OPORD.setGeometry(QtCore.QRect(0, 0, 1231, 991))
self.OPORD.setTabBarAutoHide(False)
self.OPORD.setObjectName("OPORD")
self.tab_4 = QtWidgets.QWidget()
self.tab_4.setObjectName("tab_4")
self.label = QtWidgets.QLabel(self.tab_4)
self.label.setGeometry(QtCore.QRect(280, 240, 311, 331))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("WAATS_Final.png"))
self.label.setObjectName("label")
self.OPORD.addTab(self.tab_4, "")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.ORDERS = QtWidgets.QStackedWidget(self.tab)
self.ORDERS.setGeometry(QtCore.QRect(0, 0, 1141, 1080))
self.ORDERS.setMaximumSize(QtCore.QSize(1920, 1080))
self.ORDERS.setObjectName("ORDERS")
self.stackedWidgetPage1 = QtWidgets.QWidget()
self.stackedWidgetPage1.setObjectName("stackedWidgetPage1")
self.WAATS4 = QtWidgets.QCheckBox(self.stackedWidgetPage1)
self.WAATS4.setGeometry(QtCore.QRect(390, 50, 141, 20))
self.WAATS4.setMaximumSize(QtCore.QSize(1920, 1080))
self.WAATS4.setObjectName("WAATS4")
self.label_2 = QtWidgets.QLabel(self.stackedWidgetPage1)
self.label_2.setGeometry(QtCore.QRect(0, 70, 271, 21))
self.label_2.setMaximumSize(QtCore.QSize(1920, 1080))
font = QtGui.QFont()
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.label_9 = QtWidgets.QLabel(self.stackedWidgetPage1)
self.label_9.setGeometry(QtCore.QRect(240, 0, 181, 21))
self.label_9.setMaximumSize(QtCore.QSize(1920, 1080))
self.label_9.setText("")
self.label_9.setObjectName("label_9")
self.num = QtWidgets.QLineEdit(self.stackedWidgetPage1)
self.num.setGeometry(QtCore.QRect(0, 30, 181, 31))
self.num.setMaximumSize(QtCore.QSize(1920, 1080))
self.num.setObjectName("num")
self.aoi = QtWidgets.QPlainTextEdit(self.stackedWidgetPage1)
self.aoi.setGeometry(QtCore.QRect(0, 110, 431, 71))
self.aoi.setMaximumSize(QtCore.QSize(1920, 1080))
self.aoi.setObjectName("aoi")
self.COPORD = QtWidgets.QPushButton(self.widget)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.OPORD.setTabText(self.OPORD.indexOf(self.tab_4), _translate("Dialog", "Welcome"))
self.WAATS4.setText(_translate("Dialog", "TASS"))
self.label_2.setText(_translate("Dialog", "Situation"))
self.num.setPlaceholderText(_translate("Dialog", "##-##"))
self.aoi.setPlainText(_translate("Dialog", "?"))
self.aoi.setPlaceholderText(_translate("Dialog", "AI"))
self.label_3.setText(_translate("Dialog", "Area of Interest"))
self.AO.setText(_translate("Dialog", "Area of Operation"))
self.ao.setPlaceholderText(_translate("Dialog", "AO"))
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_())
as I see on your code, to get string from the QLineEdit you can use this code.
self.num.text()
if you print that code, it will automatically print the inputed string of QLineEdit.
you can add a QPushButton then make a signal slot to do that as a form in commonly use.
like these:
self.button = QPushButton()
self.button.clicked.connect(self.getLineEdit)
def getLineEdit(self):
lineEditText = self.num.text()
print(lineEditText)

Getting an error while trying to close and open current dialog window in python

SignIn.py
from PyQt5 import QtCore, QtGui, QtWidgets
from HomePage import Ui_HomePage
from PyQt5.QtWidgets import QMessageBox
import cx_Oracle
class Ui_SignIn(object):
def openConnection(self):
con=cx_Oracle.connect('lab/oracle#localhost:1521/xe')
def showMessageBox(self,title,message):
#QMessageBox.about(self, "Title", "Message")
msgBox = QtWidgets.QMessageBox()
msgBox.setIcon(QtWidgets.QMessageBox.Warning)
msgBox.setWindowTitle(title)
msgBox.setText(message)
msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
msgBox.exec_()
def loginCheck(self):
username = self.ledt_username.text()
password = self.ledt_password.text()
#print(username)
#print(password)
con=cx_Oracle.connect('lab/oracle#localhost:1521/xe')
cur=con.cursor()
cur.execute('select * from user_tbl where username=:1 and password=:2', (username, password))
#cur.execute('select * from user_tbl')
cur.fetchall()
#n=0
#for result in cur:
#n=n+1
#m=cur.rowcount
#print(n)
#print(m)
if(cur.rowcount > 0):
print("User Found ! ")
Dialog.hide()
self.welcomeWindowShow()
else:
print("User Not Found !")
#self.showMessageBox()
self.showMessageBox('Warning','Invalid Username And Password')
#self.welcomeWindowShow()
con.close()
def welcomeWindowShow(self):
self.window=QtWidgets.QDialog()
self.ui=Ui_HomePage()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(690, 450)
Dialog.setBaseSize(QtCore.QSize(0, 0))
self.lbl_username = QtWidgets.QLabel(Dialog)
self.lbl_username.setGeometry(QtCore.QRect(250, 100, 61, 21))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.lbl_username.setFont(font)
self.lbl_username.setLayoutDirection(QtCore.Qt.RightToLeft)
self.lbl_username.setAutoFillBackground(False)
self.lbl_username.setObjectName("lbl_username")
self.ledt_username = QtWidgets.QLineEdit(Dialog)
self.ledt_username.setGeometry(QtCore.QRect(330, 100, 141, 21))
self.ledt_username.setObjectName("ledt_username")
self.lbl_password = QtWidgets.QLabel(Dialog)
self.lbl_password.setGeometry(QtCore.QRect(250, 150, 71, 21))
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.lbl_password.setFont(font)
self.lbl_password.setAutoFillBackground(False)
self.lbl_password.setObjectName("lbl_password")
self.ledt_password = QtWidgets.QLineEdit(Dialog)
self.ledt_password.setGeometry(QtCore.QRect(330, 150, 141, 20))
self.ledt_password.setObjectName("ledt_password")
self.pbtn_signin = QtWidgets.QPushButton(Dialog)
self.pbtn_signin.setGeometry(QtCore.QRect(270, 200, 75, 23))
self.pbtn_signin.setObjectName("pbtn_signin")
############## logincheck calling ######
self.pbtn_signin.clicked.connect(self.loginCheck)
###################################################
self.pbtn_signup = QtWidgets.QPushButton(Dialog)
self.pbtn_signup.setGeometry(QtCore.QRect(380, 200, 75, 23))
self.pbtn_signup.setObjectName("pbtn_signup")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.lbl_username.setText(_translate("Dialog", "Username"))
self.lbl_password.setText(_translate("Dialog", "Password"))
self.pbtn_signin.setText(_translate("Dialog", "Sign In"))
self.pbtn_signup.setText(_translate("Dialog", "Sign Up"))
# from HomePage import Ui_HomePage
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_SignIn()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
HomePage.py
from PyQt5 import QtCore, QtGui, QtWidgets
#from SignIn import Ui_SignIn
class Ui_HomePage(object):
def importSignIn(self):
from SignIn import Ui_SignIn
def signInWindowShow(self):
Dialog.hide()
from SignIn import Ui_SignIn
self.window=QtWidgets.QDialog()
self.ui=Ui_SignIn()
self.ui.setupUi(self.window)
self.window.show()
#self.Dialog.hide()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(696, 395)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(210, 50, 161, 41))
font = QtGui.QFont()
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(300, 220, 75, 23))
self.pushButton.setObjectName("pushButton")
############## logincheck calling ######
self.pushButton.clicked.connect(self.signInWindowShow)
###################################################
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "Welcome"))
self.pushButton.setText(_translate("Dialog", "Logout"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_HomePage()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
Getting an error while trying to close and open current dialog window in python
I need to open and close two windows in loop.
also attaching the error that i got.
enter image description here
The reason of the error is that Dialog is not available in scope of loginCheck method and need to be passed explicitly as a param in the following line:
self.pbtn_signin.clicked.connect(self.loginCheck)
Here is the question with the answers, how to do it.
One of the options would be import partial from functools:
from functools import partial
And use this function to pass Dialog to loginCheck:
self.pbtn_signin.clicked.connect(partial(self.loginCheck, Dialog))

Categories