Python Forum
[PyQt] [Solved]Pass value from GUI to Another Python Script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] [Solved]Pass value from GUI to Another Python Script
#1
Hello,

I'm trying to pass my userInput value userInput = self.UserInputLineEdit.text() from my BX_GUI.py to my BX_Test.py so I can use that value to populate an appropriate response.

This is what I have in my BX_GUI.py:
    #Submit Function
    def SubmitClicked(self):
        #Take UserInput & append it to ConvoTextBrowser & Make User Input Text Color: Amber
        userInput = self.UserInputLineEdit.text()
        self.ConvoTextBrowser.append("\n")
        ConvoHistory = self.ConvoTextBrowser.append(f"<p style='color:rgb(255, 170, 0)'>{userInput}</p>")
        return userInput
And I want to take the userInput and carry it over to the BX_Test.py, so I can use that value in that script.

BX_Test.py Snippet:
#-------------------------------------------------------------------------------------
#                          User Input Function - Take Commands
#-------------------------------------------------------------------------------------
def UserInput():
    from BX_GUI import Ui_MainDisplay
    command = userInput
    return command 
#-------------------------------------------------------------------------------------
How do I carry the userInput value from my GUI to the other Python Script & what do I have to import in order to do so?

Thanks in advance.


Full BX_GUI.py Code:
#!/usr/bin/env python3

import sys
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QMainWindow, 
                                QLabel, QLineEdit, QTableWidget, QTableWidgetItem, 
                                QGridLayout, QVBoxLayout, QSizePolicy, QSpacerItem, 
                                QMessageBox,QSpinBox, QComboBox, QTableView,QStyledItemDelegate)
from PyQt5.QtCore import Qt, QMetaObject, QCoreApplication
from PyQt5.QtGui import QFont

#----------------------------------------------------------------------------------------------------
#                                      Main Display
#----------------------------------------------------------------------------------------------------
class Ui_MainDisplay(QMainWindow):
    def __init__(self, parent = None):
        super(Ui_MainDisplay, self).__init__(parent)
        self.setObjectName("MainDisplay")
        self.setFixedSize(800, 665)
        self.setStyleSheet("background-color: rgb(0, 0, 0);")
        self.centralwidget = QtWidgets.QWidget(self)
        self.centralwidget.setObjectName("centralwidget")
        self.MainFrame = QtWidgets.QFrame(self.centralwidget)
        self.MainFrame.setGeometry(QtCore.QRect(10, 70, 781, 521))
        self.MainFrame.setStyleSheet("")
        self.MainFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.MainFrame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.MainFrame.setObjectName("MainFrame")
        self.ConvoTextBrowser = QtWidgets.QTextBrowser(self.MainFrame)
        self.ConvoTextBrowser.setGeometry(QtCore.QRect(10, 20, 271, 481))
        font = QtGui.QFont()
        font.setFamily("Robo Sapien")
        font.setPointSize(10)
        self.ConvoTextBrowser.setFont(font)
        self.ConvoTextBrowser.setStyleSheet("color: rgb(0, 255, 0);")
        self.ConvoTextBrowser.setObjectName("ConvoTextBrowser")
        self.TextBrowser = QtWidgets.QTextBrowser(self.MainFrame)
        self.TextBrowser.setGeometry(QtCore.QRect(0, 10, 781, 501))
        self.TextBrowser.setStyleSheet("color: rgb(0, 170, 0);")
        self.TextBrowser.setObjectName("TextBrowser")
        self.calendarWidget = QtWidgets.QCalendarWidget(self.MainFrame)
        self.calendarWidget.setGeometry(QtCore.QRect(290, 20, 312, 183))
        self.calendarWidget.setStyleSheet("color: rgb(120, 120, 120);\n"
"selection-color: rgb(255, 149, 0);\n"
"selection-background-color: rgb(100, 100, 100);")
        self.calendarWidget.setObjectName("calendarWidget")
        self.CalendarTextBrowser = QtWidgets.QTextBrowser(self.MainFrame)
        self.CalendarTextBrowser.setGeometry(QtCore.QRect(295, 211, 311, 221))
        font = QtGui.QFont()
        font.setFamily("Runlion")
        font.setPointSize(10)
        self.CalendarTextBrowser.setFont(font)
        self.CalendarTextBrowser.setStyleSheet("color: rgb(255, 170, 0);")
        self.CalendarTextBrowser.setObjectName("CalendarTextBrowser")
        self.TODOTextBrowser = QtWidgets.QTextBrowser(self.MainFrame)
        self.TODOTextBrowser.setGeometry(QtCore.QRect(620, 210, 151, 291))
        font = QtGui.QFont()
        font.setFamily("Runlion")
        font.setPointSize(10)
        self.TODOTextBrowser.setFont(font)
        self.TODOTextBrowser.setStyleSheet("color: rgb(170, 0, 0);\n"
"color: rgb(0, 85, 255);")
        self.TODOTextBrowser.setObjectName("TODOTextBrowser")
        self.NewsTextBrowser = QtWidgets.QTextBrowser(self.MainFrame)
        self.NewsTextBrowser.setGeometry(QtCore.QRect(295, 440, 311, 61))
        font = QtGui.QFont()
        font.setFamily("Android Insomnia")
        self.NewsTextBrowser.setFont(font)
        self.NewsTextBrowser.setStyleSheet("color: rgb(255, 170, 0);")
        self.NewsTextBrowser.setObjectName("NewsTextBrowser")
        self.DateTimeTextBrowser = QtWidgets.QTextBrowser(self.MainFrame)
        self.DateTimeTextBrowser.setGeometry(QtCore.QRect(630, 20, 131, 41))
        font = QtGui.QFont()
        font.setFamily("Roblox Font")
        font.setPointSize(10)
        self.DateTimeTextBrowser.setFont(font)
        self.DateTimeTextBrowser.setStyleSheet("color: rgb(0, 170, 0);")
        self.DateTimeTextBrowser.setObjectName("DateTimeTextBrowser")
        self.BootStatusTextBrowser = QtWidgets.QTextBrowser(self.MainFrame)
        self.BootStatusTextBrowser.setGeometry(QtCore.QRect(610, 70, 171, 131))
        font = QtGui.QFont()
        font.setFamily("Robo Sapien")
        font.setItalic(True)
        self.BootStatusTextBrowser.setFont(font)
        self.BootStatusTextBrowser.setStyleSheet("color: rgb(0, 170, 0);")
        self.BootStatusTextBrowser.setObjectName("BootStatusTextBrowser")
        self.TextBrowser.raise_()
        self.ConvoTextBrowser.raise_()
        self.calendarWidget.raise_()
        self.CalendarTextBrowser.raise_()
        self.TODOTextBrowser.raise_()
        self.NewsTextBrowser.raise_()
        self.DateTimeTextBrowser.raise_()
        self.BootStatusTextBrowser.raise_()
        self.InputFrame = QtWidgets.QFrame(self.centralwidget)
        self.InputFrame.setGeometry(QtCore.QRect(110, 590, 581, 51))
        self.InputFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.InputFrame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.InputFrame.setObjectName("InputFrame")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.InputFrame)
        self.verticalLayout.setObjectName("verticalLayout")
        self.UserInputLineEdit = QtWidgets.QLineEdit(self.InputFrame)
        font = QtGui.QFont()
        font.setFamily("Android Insomnia")
        font.setPointSize(10)
        self.UserInputLineEdit.setFont(font)
        self.UserInputLineEdit.setStyleSheet("color: rgb(255, 170, 0);\n"
"")
        self.UserInputLineEdit.setObjectName("UserInputLineEdit")
        self.verticalLayout.addWidget(self.UserInputLineEdit)
        self.HeaderFrame = QtWidgets.QFrame(self.centralwidget)
        self.HeaderFrame.setGeometry(QtCore.QRect(9, 10, 781, 61))
        self.HeaderFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.HeaderFrame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.HeaderFrame.setObjectName("HeaderFrame")
        self.TitleLabel = QtWidgets.QLabel(self.HeaderFrame)
        self.TitleLabel.setGeometry(QtCore.QRect(0, 0, 781, 41))
        font = QtGui.QFont()
        font.setFamily("Android Insomnia")
        font.setPointSize(25)
        font.setBold(False)
        font.setWeight(50)
        self.TitleLabel.setFont(font)
        self.TitleLabel.setStyleSheet("color: rgb(0, 255, 0);")
        self.TitleLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.TitleLabel.setObjectName("TitleLabel")
        self.SystemStatusLabel = QtWidgets.QLabel(self.HeaderFrame)
        self.SystemStatusLabel.setGeometry(QtCore.QRect(6, 36, 771, 20))
        font = QtGui.QFont()
        font.setFamily("Android Insomnia")
        font.setPointSize(10)
        self.SystemStatusLabel.setFont(font)
        self.SystemStatusLabel.setStyleSheet("color: rgb(0, 230, 0);")
        self.SystemStatusLabel.setAlignment(QtCore.Qt.AlignCenter)
        self.SystemStatusLabel.setObjectName("SystemStatusLabel")
        self.WeatherTextBrowser = QtWidgets.QTextBrowser(self.HeaderFrame)
        self.WeatherTextBrowser.setGeometry(QtCore.QRect(620, 0, 161, 61))
        font = QtGui.QFont()
        font.setFamily("Android Insomnia")
        font.setPointSize(10)
        self.WeatherTextBrowser.setFont(font)
        self.WeatherTextBrowser.setStyleSheet("color: rgb(255, 170, 0);")
        self.WeatherTextBrowser.setObjectName("WeatherTextBrowser")
        self.graphicsView = QtWidgets.QGraphicsView(self.HeaderFrame)
        self.graphicsView.setGeometry(QtCore.QRect(560, 0, 61, 61))
        self.graphicsView.setObjectName("graphicsView")
        self.SubmitButton = QtWidgets.QPushButton(self.centralwidget)
        self.SubmitButton.setGeometry(QtCore.QRect(700, 600, 91, 41))
        font = QtGui.QFont()
        font.setFamily("Lethal Injector")
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.SubmitButton.setFont(font)
        self.SubmitButton.setStyleSheet("background-color: rgb(170, 170, 0);\n"
"background-color: rgb(200, 133, 0);")
        self.SubmitButton.setObjectName("SubmitButton")
        self.OnOffButton = QtWidgets.QPushButton(self.centralwidget)
        self.OnOffButton.setGeometry(QtCore.QRect(10, 600, 91, 41))
        font = QtGui.QFont()
        font.setFamily("Lethal Injector")
        font.setPointSize(10)
        font.setBold(True)
        font.setWeight(75)
        self.OnOffButton.setFont(font)
        self.OnOffButton.setStyleSheet("background-color: rgb(170, 0, 0);")
        self.OnOffButton.setObjectName("OnOffButton")
        self.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(self)
        self.statusbar.setObjectName("statusbar")
        self.setStatusBar(self.statusbar)

        self.retranslateUi(self)
        QtCore.QMetaObject.connectSlotsByName(self)
#----------------------------------------------------------------------------------------------------

#----------------------------------------------------------------------------------------------------
#                                       Button Actions
#----------------------------------------------------------------------------------------------------
        #------------------------------------------
                        #Submit Button
        #------------------------------------------
        #When the Submit button is clicked -> submitClicked Function
        SubmitButton = self.SubmitButton
        SubmitButton.clicked.connect(self.SubmitClicked)
        #------------------------------------------

    #Submit Function
    def SubmitClicked(self):
        #Take UserInput & append it to ConvoTextBrowser & Make User Input Text Color: Amber
        userInput = self.UserInputLineEdit.text()
        self.ConvoTextBrowser.append("\n")
        ConvoHistory = self.ConvoTextBrowser.append(f"<p style='color:rgb(255, 170, 0)'>{userInput}</p>")
        return userInput


#----------------------------------------------------------------------------------------------------
#                                      Retranslate Ui
#----------------------------------------------------------------------------------------------------
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("MainWindow", "MainWindow"))
        
        self.ConvoTextBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Robo Sapien\'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Hello Commander. How may I be of service?</p>\n"
"<p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">What is 1 + 1?</p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">1 + 1 = 2</p>\n"
"<p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Thanks.</p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">No problem.</p></body></html>"))
        
        self.CalendarTextBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Runlion\'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">August 21 2022</p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">-Dentist appointment @ 2:00pm</p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">-Doctor\'s Appointment @ 3:00pm</p></body></html>"))
        
        self.TODOTextBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Runlion\'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-weight:600; font-style:italic;\">Todo:</span></p>\n"
"<p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-weight:600; font-style:italic;\"><br /></p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-weight:600; font-style:italic;\">-Fix Anycubic Mega X PRinter</span></p>\n"
"<p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-weight:600; font-style:italic;\"><br /></p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-weight:600; font-style:italic;\">-Work on B.A.X.T.E.R</span></p></body></html>"))
        
        self.NewsTextBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Android Insomnia\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">Todays\' News</span></p>\n"
"<p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:10pt;\"><br /></p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:10pt;\">- News Headline 1</span></p></body></html>"))
        
        self.DateTimeTextBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Roblox Font\'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Date: 08/21/22</p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Time: 07:15:20pm</p></body></html>"))
        
        self.BootStatusTextBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Robo Sapien\'; font-size:8.25pt; font-weight:400; font-style:italic;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Starting Systems...</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Systems Started...</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Connecting to Databases...</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Databases - online</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Checking Connections...</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Defense Arm - Online</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">V.L.A.D 001 - Online</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">V.L.A.D 002 - Online</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Neural Network - Online</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Establishing Link...</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">Secure Link Established...</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">---</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">All Systems  Running</span></p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">---</span></p></body></html>"))
        
        self.TitleLabel.setText(_translate("MainWindow", "B.A.X.T.E.R"))
        self.SystemStatusLabel.setText(_translate("MainWindow", "Systems Online"))
        
        self.WeatherTextBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Android Insomnia\'; font-size:10pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">ThunderStorms</p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">22.9 Celcius</p>\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">Feels Like: 23.4</p></body></html>"))
        
        self.SubmitButton.setText(_translate("MainWindow", "Submit"))
        self.OnOffButton.setText(_translate("MainWindow", "Mic Offline"))
#----------------------------------------------------------------------------------------------------


#----------------------------------------------------------------------------------------------------
#                                       Run this Program
#----------------------------------------------------------------------------------------------------
def main():
    app = QApplication(sys.argv)
    win = Ui_MainDisplay()
    win.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()
#----------------------------------------------------------------------------------------------------
Reply
#2
You could rewrite your BX_GUI.main() function to return the value. Here's a simple example

window.py
from PySide6 import QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        layout = QtWidgets.QVBoxLayout(self)
        self.edit = QtWidgets.QLineEdit(self)
        layout.addChildWidget(self.edit)

def draw():
    app = QtWidgets.QApplication()
    window = Window()
    window.show()
    app.exec()
    return window.edit.text()
Test.py
import window

print(window.draw())
This treats the window like a dialog for the test program. The test program launches the window and waits until the window closes.
Reply
#3
Ok so I re-wrote the BX_GUI.main():
#----------------------------------------------------------------------------------------------------
#                                       Run this Program
#----------------------------------------------------------------------------------------------------
def main():
    app = QApplication(sys.argv)
    win = Ui_MainDisplay()
    win.show()
    app.exec()
    return win.UserInputLineEdit.text()
#     sys.exit(app.exec_())

if __name__ == "__main__":
    main()
#----------------------------------------------------------------------------------------------------
And Imported it into BX_Test.py so I can use the returned value as a command so it can be processed, But it only processes the command when
I close/exit the GUI not when I click the submit button.

Is there a way to fix this so it returns the value once I click the submit button, not when the program closes?
Reply
#4
Not really. The GUI blocks until it is closed. If you launch the GUI from your "test", the test has to wait until the GUI closes.

You could reverse the dependency. The GUI can call your test. It sounds like that is the proper way to organize the code anyway.
Reply
#5
Thanks. So I revered the dependencies and got it to carry over the UserInput Value when the submit button is clicked and have BX_Test.py print the value to confirm it received it, but now my BX_Test.py doesn't do anything with the value.


BX_GUI.py
    #Submit Function
    def SubmitClicked(self):
        #Take UserInput & append it to ConvoTextBrowser & Make User Input Text Color: Amber
        userInput = self.UserInputLineEdit.text()
        import BX_Test
        BX_Test.UserInput(userInput)
        self.ConvoTextBrowser.append("\n")
        ConvoHistory = self.ConvoTextBrowser.append(f"<p style='color:rgb(255, 170, 0)'>{userInput}</p>")
BX_Test.py:
#-------------------------------------------------------------------------------------
#                                User Input Function
#-------------------------------------------------------------------------------------
def UserInput(userInputCommand):
    #command = BX_GUI.main()
    print("User Command: " + userInputCommand)
    command = userInputCommand
    #command = str(Prompt.ask("[yellow]Commander[/yellow]"))
    return command 
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
#                                       Main
#-------------------------------------------------------------------------------------
def BAXTER():
    command = UserInput() 
    command=str(command).lower()

    #-------------------------------------------------------------------------------------
                            #General Conversation (From Intents.py) 
    #-------------------------------------------------------------------------------------
    #Greetings
    patterns, responses = greetings()
    if (command in patterns):
        response = (random.choice(responses))
        #----------------------
        #Auto typing animation:
        print("[green]Baxter: [/green]", end="")
        for i in response:
            sys.stdout.write(i)
            sys.stdout.flush()
            time.sleep(0.1)
        print("\n")
        #----------------------
        speak(response)

    #Farewell
    patterns, responses = farewell()
    if (command in patterns):
        response = (random.choice(responses))
        #----------------------
        #Auto typing animation:
        print("[green]Baxter: [/green]", end="")
        for i in response:
            sys.stdout.write(i)
            sys.stdout.flush()
            time.sleep(0.1)
        print("\n")
        #----------------------
        speak(response)

    #-------------------------------------------------------------------------------------
                            #Search Wikipedia (General Info)
    #-------------------------------------------------------------------------------------
    if ('weather' not in command):
        if ('who is' in command) or ('what is the' in command) or ('what is a' in command) or ("what is" in command):
            if ('time' not in command):
                if ('news' not in command):
                    speak('Searching Wikipedia...')
                    command = command.replace("who is","")
                    command = command.replace("what is the","")
                    command = command.replace("what is a","")
                    command = command.replace("what is","")
                    results = wikipedia.summary(command, sentences = 2)
                    #----------------------
                    #Auto typing animation:
                    print("[green]Baxter: [/green]", end="")
                    for i in results:
                        sys.stdout.write(i)
                        sys.stdout.flush()
                        time.sleep(0.05)
                    print("\n")
                    #----------------------
                    speak(results) 


#------------------------------------------------------------------------------------------
#                                   Run The Program
#------------------------------------------------------------------------------------------
def main():
    speak("How may I be of service?") 
    
    while True:
        BAXTER()

if __name__ == "__main__":
    main()
#------------------------------------------------------------------------------------------
So If I type in "What is a tower" in the GUI and hit submit, BX_Test.py will print ""What is a tower" but it won't process the command (jump to the Wikipedia if statement and search Wikipedia for the answer). Instead, it does nothing.

How can I fix this?

Thanks again.

Attached Files

Thumbnail(s)
   
Reply
#6
Other than "Well, duh?" I'm not sure how to answer this one.

This is what happens when you press the button in the GUI.
def UserInput(userInputCommand):
    #command = BX_GUI.main()  # Nothing
    print("User Command: " + userInputCommand)  # print
    command = userInputCommand  # Asssignment, but doesn't really do anything
    #command = str(Prompt.ask("[yellow]Commander[/yellow]"))  # Commented out, so nothing
    return command  # Returns something to GUI which is not used 
Where are you doing anything other than printing the command? What do you want to happen?

I foresee difficulties in your future. In the GUI program you call exec() which is an event manager that runs in a loop until the main window is closed. In your BX_test.main you have:
    while True:
        BAXTER()
]
which wants to loop forever. You have two things that want to run all the time and they cannot do that in one process without using threads. If you run them as two separate processes, you'll have to implement some kind of inter-process communication. This is going to be a steep learning curve.
Reply
#7
Read about QThread
Use PyQt's QThread to Prevent Freezing GUIs
Reply
#8
To get it to work I just took the code from BX_Test.py and combined it into BX_GUI.py so it's all together.
It now works as intended.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to pass args from GUI to pre-written script. Travolta 2 2,821 Aug-08-2017, 06:09 PM
Last Post: Travolta

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020