How to call QPushButton and QLineEdit from window 2
-
Hello, I have python app, where I wan't to add login window called from main GUI. Extra window has two QLineEdit fields and one QPushButton. Now I can succesfully call login window with button from main GUI, but I don't know how to access button and lineedits from login window. Becouse of a very big amounth of the code I will post only parts of the code of main.py and gui.py and complete code of login_window.py. Hope that explanation is satisfying.
Regards, Vlado
... master.pushButton_Login.clicked.connect(master.open_Login_Window) ...from PyQt5 import QtCore, QtGui, QtWidgets from login_window import Ui_Dialog class Ui_SCADA(object): def open_Login_Window(self): self.window = QtWidgets.QDialog() self.ui = Ui_Dialog() self.ui.setupUi(self.window) self.window.show() def setupUi(self, SCADA): SCADA.setObjectName("SCADA") SCADA.resize(888, 665)login_window.py:
from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 300) self.bgwidget = QtWidgets.QWidget(Dialog) self.bgwidget.setGeometry(QtCore.QRect(-1, -1, 401, 301)) self.bgwidget.setStyleSheet("QWidget#bgwidget{\n" "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));}") self.bgwidget.setObjectName("bgwidget") self.label = QtWidgets.QLabel(self.bgwidget) self.label.setGeometry(QtCore.QRect(160, 30, 91, 31)) font = QtGui.QFont() font.setPointSize(20) self.label.setFont(font) self.label.setStyleSheet("color: rgb(255, 255, 127);") self.label.setObjectName("label") self.pushButton_prijava = QtWidgets.QPushButton(self.bgwidget) self.pushButton_prijava.setGeometry(QtCore.QRect(140, 230, 121, 30)) self.pushButton_prijava.setStyleSheet("color: rgb(255, 255, 127);\n" "background-color: rgb(99, 99, 99);") self.pushButton_prijava.setObjectName("pushButton_prijava") self.lineEdit_user_name = QtWidgets.QLineEdit(self.bgwidget) self.lineEdit_user_name.setGeometry(QtCore.QRect(140, 110, 121, 30)) self.lineEdit_user_name.setObjectName("lineEdit_user_name") self.lineEdit_password = QtWidgets.QLineEdit(self.bgwidget) self.lineEdit_password.setGeometry(QtCore.QRect(140, 170, 121, 30)) self.lineEdit_password.setObjectName("lineEdit_password") self.label_2 = QtWidgets.QLabel(self.bgwidget) self.label_2.setGeometry(QtCore.QRect(60, 120, 68, 22)) self.label_2.setStyleSheet("color: rgb(255, 255, 127);") self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.bgwidget) self.label_3.setGeometry(QtCore.QRect(60, 180, 68, 22)) self.label_3.setStyleSheet("color: rgb(255, 255, 127);") self.label_3.setObjectName("label_3") 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", "Prijava")) self.pushButton_prijava.setText(_translate("Dialog", "Prijava")) self.label_2.setText(_translate("Dialog", "Up. ime")) self.label_3.setText(_translate("Dialog", "Geslo")) 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_()) -
Hi,
The correct way to do this is to give your dialogs proper accessor method that will return the content of the line edits you have in there.
The widgets in your dialog are an implementation detail for the main window and thus your dialog should have a simple api that returns the data.
-
Thanks for your respond. Can you be more specific, couse Iam not an expert with python. Some example would be very appreciatted.
Regards
-
There are lots of examples in the Qt for Python documentation like this one.
-
Well, I know it must be defined in this part of the code below, I just don't know how. Would you be so kind to writte few lines for Button, for example?
class Ui_SCADA(object): def open_Login_Window(self): self.window = QtWidgets.QDialog() self.ui = Ui_Dialog() self.ui.setupUi(self.window) self.window.show() def setupUi(self, SCADA): SCADA.setObjectName("SCADA") SCADA.resize(888, 665) -
Well, I know it must be defined in this part of the code below, I just don't know how. Would you be so kind to writte few lines for Button, for example?
class Ui_SCADA(object): def open_Login_Window(self): self.window = QtWidgets.QDialog() self.ui = Ui_Dialog() self.ui.setupUi(self.window) self.window.show() def setupUi(self, SCADA): SCADA.setObjectName("SCADA") SCADA.resize(888, 665)@California There is really not much to it:
class Ui_Dialog(object): ... def userName(self): return self.lineEdit_user_name.text() # Same for all other text edits you need -
Thanks for help.
Ok, for my knowledge is quite big chalenge. It works now in second window (code below), but how to pass variable un into main?# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'login.ui' # # Created by: PyQt5 UI code generator 5.15.2 # # 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 try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_Dialog(object): def user_Name(self): un = self.lineEdit_user_name.text() print(un) return un def pass_Word(self): pw = self.lineEdit_password.text() print(pw) return pw def prijava(self): print("Prijava") self.user_Name() self.pass_Word() def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 300) self.bgwidget = QtWidgets.QWidget(Dialog) self.bgwidget.setGeometry(QtCore.QRect(-1, -1, 401, 301)) self.bgwidget.setStyleSheet("QWidget#bgwidget{\n" "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));}") self.bgwidget.setObjectName("bgwidget") self.label = QtWidgets.QLabel(self.bgwidget) self.label.setGeometry(QtCore.QRect(160, 30, 91, 31)) font = QtGui.QFont() font.setPointSize(20) self.label.setFont(font) self.label.setStyleSheet("color: rgb(255, 255, 127);") self.label.setObjectName("label") #self.pushButton_Log_in = QtWidgets.QPushButton(self.bgwidget) self.pushButton_Log_in = QtWidgets.QPushButton(self.bgwidget, clicked = lambda: self.prijava()) self.pushButton_Log_in.setGeometry(QtCore.QRect(140, 230, 121, 30)) self.pushButton_Log_in.setStyleSheet("color: rgb(255, 255, 127);\n" "background-color: rgb(99, 99, 99);") self.pushButton_Log_in.setObjectName("pushButton_Log_in") self.lineEdit_user_name = QtWidgets.QLineEdit(self.bgwidget) self.lineEdit_user_name.setGeometry(QtCore.QRect(140, 110, 121, 30)) self.lineEdit_user_name.setObjectName("lineEdit_user_name") self.lineEdit_password = QtWidgets.QLineEdit(self.bgwidget) #self.lineEdit_password = QtWidgets.setEchoMode.QLineEdit(self.bgwidget) self.lineEdit_password.setGeometry(QtCore.QRect(140, 170, 121, 30)) self.lineEdit_password.setObjectName("lineEdit_password") self.label_2 = QtWidgets.QLabel(self.bgwidget) self.label_2.setGeometry(QtCore.QRect(60, 120, 68, 22)) self.label_2.setStyleSheet("color: rgb(255, 255, 127);") self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.bgwidget) self.label_3.setGeometry(QtCore.QRect(60, 180, 68, 22)) self.label_3.setStyleSheet("color: rgb(255, 255, 127);") self.label_3.setObjectName("label_3") 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", "Prijava")) self.pushButton_Log_in.setText(_translate("Dialog", "Prijava")) self.label_2.setText(_translate("Dialog", "Up. ime")) self.label_3.setText(_translate("Dialog", "Geslo")) 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_()) -
Thanks for help.
Ok, for my knowledge is quite big chalenge. It works now in second window (code below), but how to pass variable un into main?# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'login.ui' # # Created by: PyQt5 UI code generator 5.15.2 # # 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 try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_Dialog(object): def user_Name(self): un = self.lineEdit_user_name.text() print(un) return un def pass_Word(self): pw = self.lineEdit_password.text() print(pw) return pw def prijava(self): print("Prijava") self.user_Name() self.pass_Word() def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 300) self.bgwidget = QtWidgets.QWidget(Dialog) self.bgwidget.setGeometry(QtCore.QRect(-1, -1, 401, 301)) self.bgwidget.setStyleSheet("QWidget#bgwidget{\n" "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));}") self.bgwidget.setObjectName("bgwidget") self.label = QtWidgets.QLabel(self.bgwidget) self.label.setGeometry(QtCore.QRect(160, 30, 91, 31)) font = QtGui.QFont() font.setPointSize(20) self.label.setFont(font) self.label.setStyleSheet("color: rgb(255, 255, 127);") self.label.setObjectName("label") #self.pushButton_Log_in = QtWidgets.QPushButton(self.bgwidget) self.pushButton_Log_in = QtWidgets.QPushButton(self.bgwidget, clicked = lambda: self.prijava()) self.pushButton_Log_in.setGeometry(QtCore.QRect(140, 230, 121, 30)) self.pushButton_Log_in.setStyleSheet("color: rgb(255, 255, 127);\n" "background-color: rgb(99, 99, 99);") self.pushButton_Log_in.setObjectName("pushButton_Log_in") self.lineEdit_user_name = QtWidgets.QLineEdit(self.bgwidget) self.lineEdit_user_name.setGeometry(QtCore.QRect(140, 110, 121, 30)) self.lineEdit_user_name.setObjectName("lineEdit_user_name") self.lineEdit_password = QtWidgets.QLineEdit(self.bgwidget) #self.lineEdit_password = QtWidgets.setEchoMode.QLineEdit(self.bgwidget) self.lineEdit_password.setGeometry(QtCore.QRect(140, 170, 121, 30)) self.lineEdit_password.setObjectName("lineEdit_password") self.label_2 = QtWidgets.QLabel(self.bgwidget) self.label_2.setGeometry(QtCore.QRect(60, 120, 68, 22)) self.label_2.setStyleSheet("color: rgb(255, 255, 127);") self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.bgwidget) self.label_3.setGeometry(QtCore.QRect(60, 180, 68, 22)) self.label_3.setStyleSheet("color: rgb(255, 255, 127);") self.label_3.setObjectName("label_3") 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", "Prijava")) self.pushButton_Log_in.setText(_translate("Dialog", "Prijava")) self.label_2.setText(_translate("Dialog", "Up. ime")) self.label_3.setText(_translate("Dialog", "Geslo")) 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_())@California said in How to call QPushButton and QLineEdit from window 2:
but how to pass variable un into main?
Please explain better.
What variable and what is "main"? -
Thanks for help.
Ok, for my knowledge is quite big chalenge. It works now in second window (code below), but how to pass variable un into main?# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'login.ui' # # Created by: PyQt5 UI code generator 5.15.2 # # 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 try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_Dialog(object): def user_Name(self): un = self.lineEdit_user_name.text() print(un) return un def pass_Word(self): pw = self.lineEdit_password.text() print(pw) return pw def prijava(self): print("Prijava") self.user_Name() self.pass_Word() def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 300) self.bgwidget = QtWidgets.QWidget(Dialog) self.bgwidget.setGeometry(QtCore.QRect(-1, -1, 401, 301)) self.bgwidget.setStyleSheet("QWidget#bgwidget{\n" "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));}") self.bgwidget.setObjectName("bgwidget") self.label = QtWidgets.QLabel(self.bgwidget) self.label.setGeometry(QtCore.QRect(160, 30, 91, 31)) font = QtGui.QFont() font.setPointSize(20) self.label.setFont(font) self.label.setStyleSheet("color: rgb(255, 255, 127);") self.label.setObjectName("label") #self.pushButton_Log_in = QtWidgets.QPushButton(self.bgwidget) self.pushButton_Log_in = QtWidgets.QPushButton(self.bgwidget, clicked = lambda: self.prijava()) self.pushButton_Log_in.setGeometry(QtCore.QRect(140, 230, 121, 30)) self.pushButton_Log_in.setStyleSheet("color: rgb(255, 255, 127);\n" "background-color: rgb(99, 99, 99);") self.pushButton_Log_in.setObjectName("pushButton_Log_in") self.lineEdit_user_name = QtWidgets.QLineEdit(self.bgwidget) self.lineEdit_user_name.setGeometry(QtCore.QRect(140, 110, 121, 30)) self.lineEdit_user_name.setObjectName("lineEdit_user_name") self.lineEdit_password = QtWidgets.QLineEdit(self.bgwidget) #self.lineEdit_password = QtWidgets.setEchoMode.QLineEdit(self.bgwidget) self.lineEdit_password.setGeometry(QtCore.QRect(140, 170, 121, 30)) self.lineEdit_password.setObjectName("lineEdit_password") self.label_2 = QtWidgets.QLabel(self.bgwidget) self.label_2.setGeometry(QtCore.QRect(60, 120, 68, 22)) self.label_2.setStyleSheet("color: rgb(255, 255, 127);") self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.bgwidget) self.label_3.setGeometry(QtCore.QRect(60, 180, 68, 22)) self.label_3.setStyleSheet("color: rgb(255, 255, 127);") self.label_3.setObjectName("label_3") 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", "Prijava")) self.pushButton_Log_in.setText(_translate("Dialog", "Prijava")) self.label_2.setText(_translate("Dialog", "Up. ime")) self.label_3.setText(_translate("Dialog", "Geslo")) 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_())Dialog.exec_() print(Dialog.user_Name()Please use proper casing for your method, variable and class names. Although it doesn't matter for your machine, the humans reading your code will have a very hard time.
Follow the PEP8 document
- Variable names in lower case
- function name in snake_case, no uppercase mixed in there
Qt uses camelCase for its functions because they come from the C++ style guide lines they are using although you can change that with the latest version of PySide6.
Beside that, since as you said you are a beginner, you should take the time to read the documentation and tutorials provided by Qt's documentation. You'll learn quite a lot there to get the basics going properly.
-
I couldnt imagine, ading extra window for log in could make so big troubles for me.
I'ii post top parts of the code for modules: main.py, gui_91.py and login_window.py so you can get the picture, couse obviously I can't explain correct what I would like to do. There are some other modules involved, but they are not important for description of my problem. Thanks for all your help and please forgive me my poor knowledge, programming is my free time pleassure.main.py (gui_91.py is imported here)
import RPi.GPIO as GPIO from subprocess import call import gui_91 import sys import os from datetime import datetime from slaves import * from Buffer_script_02 import * from graph_08c import * from kalkulacije import * from xls_store_8 import* from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QMainWindow, QApplication from PyQt5.QtCore import QTimer import time ...gui_91.py (login_window.py is imported here)
from PyQt5 import QtCore, QtGui, QtWidgets from login_window import Ui_Dialog try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_SCADA(object): #----------------------------------------------------------------------------------------------- def open_Login_Window(self): self.window = QtWidgets.QDialog() self.ui = Ui_Dialog() self.ui.setupUi(self.window) self.window.show() #----------------------------------------------------------------------------------------------- def setupUi(self, SCADA): SCADA.setObjectName("SCADA") SCADA.resize(888, 665) ...login_window.py (I want to use methods "user_Name" and "pass_Word" in main.py or in gui_91.py)
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'login.ui' # # Created by: PyQt5 UI code generator 5.15.2 # # 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 try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_Dialog(object): def user_Name(self): un = self.lineEdit_user_name.text() print(un) return un def pass_Word(self): pw = self.lineEdit_password.text() print(pw) return pw def log_IN(self): print("Log In") self.user_Name() self.pass_Word() def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 300) self.bgwidget = QtWidgets.QWidget(Dialog) self.bgwidget.setGeometry(QtCore.QRect(-1, -1, 401, 301)) self.bgwidget.setStyleSheet("QWidget#bgwidget{\n" "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));}") self.bgwidget.setObjectName("bgwidget") self.label = QtWidgets.QLabel(self.bgwidget) self.label.setGeometry(QtCore.QRect(160, 30, 91, 31)) font = QtGui.QFont() font.setPointSize(20) self.label.setFont(font) self.label.setStyleSheet("color: rgb(255, 255, 127);") self.label.setObjectName("label") #self.pushButton_Log_in = QtWidgets.QPushButton(self.bgwidget) self.pushButton_Log_in = QtWidgets.QPushButton(self.bgwidget, clicked = lambda: log_IN()) self.pushButton_Log_in.setGeometry(QtCore.QRect(140, 230, 121, 30)) self.pushButton_Log_in.setStyleSheet("color: rgb(255, 255, 127);\n" "background-color: rgb(99, 99, 99);") self.pushButton_Log_in.setObjectName("pushButton_Log_in") self.lineEdit_user_name = QtWidgets.QLineEdit(self.bgwidget) self.lineEdit_user_name.setGeometry(QtCore.QRect(140, 110, 121, 30)) self.lineEdit_user_name.setObjectName("lineEdit_user_name") self.lineEdit_password = QtWidgets.QLineEdit(self.bgwidget) self.lineEdit_password.setGeometry(QtCore.QRect(140, 170, 121, 30)) self.lineEdit_password.setObjectName("lineEdit_password") self.label_2 = QtWidgets.QLabel(self.bgwidget) self.label_2.setGeometry(QtCore.QRect(60, 120, 68, 22)) self.label_2.setStyleSheet("color: rgb(255, 255, 127);") self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.bgwidget) self.label_3.setGeometry(QtCore.QRect(60, 180, 68, 22)) self.label_3.setStyleSheet("color: rgb(255, 255, 127);") self.label_3.setObjectName("label_3") 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", "Prijava")) self.pushButton_Log_in.setText(_translate("Dialog", "Prijava")) self.label_2.setText(_translate("Dialog", "Up. ime")) self.label_3.setText(_translate("Dialog", "Geslo")) 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 couldnt imagine, ading extra window for log in could make so big troubles for me.
I'ii post top parts of the code for modules: main.py, gui_91.py and login_window.py so you can get the picture, couse obviously I can't explain correct what I would like to do. There are some other modules involved, but they are not important for description of my problem. Thanks for all your help and please forgive me my poor knowledge, programming is my free time pleassure.main.py (gui_91.py is imported here)
import RPi.GPIO as GPIO from subprocess import call import gui_91 import sys import os from datetime import datetime from slaves import * from Buffer_script_02 import * from graph_08c import * from kalkulacije import * from xls_store_8 import* from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QMainWindow, QApplication from PyQt5.QtCore import QTimer import time ...gui_91.py (login_window.py is imported here)
from PyQt5 import QtCore, QtGui, QtWidgets from login_window import Ui_Dialog try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_SCADA(object): #----------------------------------------------------------------------------------------------- def open_Login_Window(self): self.window = QtWidgets.QDialog() self.ui = Ui_Dialog() self.ui.setupUi(self.window) self.window.show() #----------------------------------------------------------------------------------------------- def setupUi(self, SCADA): SCADA.setObjectName("SCADA") SCADA.resize(888, 665) ...login_window.py (I want to use methods "user_Name" and "pass_Word" in main.py or in gui_91.py)
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'login.ui' # # Created by: PyQt5 UI code generator 5.15.2 # # 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 try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtGui.QApplication.UnicodeUTF8 def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig, _encoding) except AttributeError: def _translate(context, text, disambig): return QtGui.QApplication.translate(context, text, disambig) class Ui_Dialog(object): def user_Name(self): un = self.lineEdit_user_name.text() print(un) return un def pass_Word(self): pw = self.lineEdit_password.text() print(pw) return pw def log_IN(self): print("Log In") self.user_Name() self.pass_Word() def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 300) self.bgwidget = QtWidgets.QWidget(Dialog) self.bgwidget.setGeometry(QtCore.QRect(-1, -1, 401, 301)) self.bgwidget.setStyleSheet("QWidget#bgwidget{\n" "background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));}") self.bgwidget.setObjectName("bgwidget") self.label = QtWidgets.QLabel(self.bgwidget) self.label.setGeometry(QtCore.QRect(160, 30, 91, 31)) font = QtGui.QFont() font.setPointSize(20) self.label.setFont(font) self.label.setStyleSheet("color: rgb(255, 255, 127);") self.label.setObjectName("label") #self.pushButton_Log_in = QtWidgets.QPushButton(self.bgwidget) self.pushButton_Log_in = QtWidgets.QPushButton(self.bgwidget, clicked = lambda: log_IN()) self.pushButton_Log_in.setGeometry(QtCore.QRect(140, 230, 121, 30)) self.pushButton_Log_in.setStyleSheet("color: rgb(255, 255, 127);\n" "background-color: rgb(99, 99, 99);") self.pushButton_Log_in.setObjectName("pushButton_Log_in") self.lineEdit_user_name = QtWidgets.QLineEdit(self.bgwidget) self.lineEdit_user_name.setGeometry(QtCore.QRect(140, 110, 121, 30)) self.lineEdit_user_name.setObjectName("lineEdit_user_name") self.lineEdit_password = QtWidgets.QLineEdit(self.bgwidget) self.lineEdit_password.setGeometry(QtCore.QRect(140, 170, 121, 30)) self.lineEdit_password.setObjectName("lineEdit_password") self.label_2 = QtWidgets.QLabel(self.bgwidget) self.label_2.setGeometry(QtCore.QRect(60, 120, 68, 22)) self.label_2.setStyleSheet("color: rgb(255, 255, 127);") self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.bgwidget) self.label_3.setGeometry(QtCore.QRect(60, 180, 68, 22)) self.label_3.setStyleSheet("color: rgb(255, 255, 127);") self.label_3.setObjectName("label_3") 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", "Prijava")) self.pushButton_Log_in.setText(_translate("Dialog", "Prijava")) self.label_2.setText(_translate("Dialog", "Up. ime")) self.label_3.setText(_translate("Dialog", "Geslo")) 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_())@California You simply call these methods as you would do with any other method (also @SGaist already shown how):
def open_Login_Window(self): self.window = QtWidgets.QDialog() self.ui = Ui_Dialog() self.ui.setupUi(self.window) self.window.show() ... name = self.ui.user_Name() password = self.ui.pass_Word()You also should use exec() instead of show() I think.
-
First of all, thanks to all for helping ...
Ok, I got one step closer...it works, but only at start up application. To represent the situation, I put together small example, which you can run...so, what to do in existing example to print both line edits content whenever is login window opened. Thanks in advance, Vlado
main.pyimport gui import logwindow import sys import os from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QMainWindow, QApplication from PyQt5.QtCore import QTimer import time #------------------------------------------------------------------------------ def userName(): un = login.lineEdit_user_name.text() print(un) return un def passWord(): pw = login.lineEdit_password.text() print(pw) return pw def log_IN(): userName() passWord() def show_login_window(): log_IN() print("-------") #----------------------------------------------------------------------------- def push_buttons(): master.pushButton_call_window.clicked.connect(master.open_Login_Window) login.pushButton_Login.clicked.connect(show_login_window) #--------------------------------------------------------------------------------- def triger(): push_buttons() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) gui_master = gui.QtWidgets.QWidget() master = gui.Ui_Form() master.setupUi(gui_master) gui_login = logwindow.QtWidgets.QDialog() login = logwindow.Ui_Dialog() login.setupUi(gui_login) timer = QTimer() timer.timeout.connect(triger) timer.start(1000) gui_master.show() gui_login.show() sys.exit(app.exec_())from PyQt5 import QtCore, QtGui, QtWidgets from logwindow import Ui_Dialog class Ui_Form(object): def open_Login_Window(self): self.window = QtWidgets.QDialog() self.ui = Ui_Dialog() self.ui.setupUi(self.window) self.window.show() def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 300) self.pushButton_call_window = QtWidgets.QPushButton(Form) self.pushButton_call_window.setGeometry(QtCore.QRect(130, 130, 141, 30)) self.pushButton_call_window.setObjectName("pushButton_call_window") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton_call_window.setText(_translate("Form", "Call Login Window")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_())# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'logwindow.ui' # # Created by: PyQt5 UI code generator 5.15.2 # # 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 class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(400, 300) self.pushButton_Login = QtWidgets.QPushButton(Dialog) self.pushButton_Login.setGeometry(QtCore.QRect(140, 200, 91, 30)) self.pushButton_Login.setObjectName("pushButton_Login") self.lineEdit_user_name = QtWidgets.QLineEdit(Dialog) self.lineEdit_user_name.setGeometry(QtCore.QRect(130, 70, 113, 30)) self.lineEdit_user_name.setObjectName("lineEdit_user_name") self.lineEdit_user_name.setText("user name") self.lineEdit_password = QtWidgets.QLineEdit(Dialog) self.lineEdit_password.setGeometry(QtCore.QRect(130, 120, 113, 30)) self.lineEdit_password.setObjectName("lineEdit_password") self.lineEdit_password.setText("password") self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.pushButton_Login.setText(_translate("Dialog", "Login")) 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_()) -
Make a proper widget of your login dialog and use it at the right place in your main window.
-
Thanks man for showing me the way, it works now. All "Log IN" actions were put into logwindow.py and called from there.
How can I mark this post as solved?Regards, Vlado
-
You can either use the "Topic Tools" button or the three dotted menu beside the answer you deem correct.