On Monday, 2 September 2019 06:02:58 UTC+10, Spencer Du wrote: > Hi > > I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a > GUI file if the file exists in current directory. I want to add the file name > to a list when a file is imported and for each subsequent file that is > imported I want the file name to be imported to the same list and print the > list or create a new list but with the imported file named added to list > which has the existing file names that have already been imported. I was > wondering how I do this. By the way run GUI.py to test this and test1.py and > test2.py are the files which can be used to import GUI . > > GUI.py > > import logging > from datetime import timedelta > import time > from thespian.actors import * > from transitions import Machine > import paho.mqtt.client as mqtt > import importlib > import os.path > import sys > from PyQt5.QtWidgets import * > from PyQt5.QtCore import * > from PyQt5 import QtWidgets, uic > from mqtt import * > import json > > class MainWindow(QtWidgets.QMainWindow): > def __init__(self,parent = None): > QMainWindow.__init__(self) > super(MainWindow, self).__init__(parent) > self.mdi = QMdiArea() > self.setCentralWidget(self.mdi) > > self.setMinimumSize(QSize(800, 600)) > self.setWindowTitle("PyQt button example - > pythonprogramminglanguage.com") > > pybutton = QPushButton('Add device', self) > > pybutton.clicked.connect(self.importbutton) > > pybutton.move(100, 400) > pybutton.resize(150, 32) > > self.textbox = QLineEdit(self) > self.textbox.move(100,350) > self.textbox.resize(100, 32) > > self.fileName_UI = "" > > def importbutton(self): > self.fileName_UI = self.textbox.text() > self.loadGUI() > > def getGUIFilename(self): > return self.fileName_UI > > def loadGUI(self): > print("Searching file", self.fileName_UI) > try: > module = __import__(self.fileName_UI) > my_class = getattr(module, "SubWindow") > > sub = QMdiSubWindow() > > sub.setWidget(my_class()) > sub.setWindowTitle("New GUI: " + self.fileName_UI) > self.mdi.addSubWindow(sub) > sub.show() > > print("creating new instance " + self.fileName_UI) > client = device("Device") > client.run() > > client.loop_start() # start the loop > device_message = self.fileName_UI > time.sleep(2) > print("Subscribing to topic", > "microscope/light_sheet_microscope/UI") > client.subscribe("microscope/light_sheet_microscope/UI") > print("Publishing message to topic", > "microscope/light_sheet_microscope/UI") > client.publish("microscope/light_sheet_microscope/UI", > json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": > "adding device"}}, indent=2)) > time.sleep(1) # wait > client.loop_stop() # stop the loop > print("Device added" + "\n") > listofdevice = [] > listofdevice.append(self.fileName_UI) > print(listofdevice) > except: > print("creating new instance " + self.fileName_UI) > client = device("Device") > client.run() > > client.loop_start() # start the loop > device_message = self.fileName_UI > time.sleep(2) > print("Subscribing to topic", > "microscope/light_sheet_microscope/UI") > client.subscribe("microscope/light_sheet_microscope/UI") > print("Publishing message to topic", > "microscope/light_sheet_microscope/UI") > client.publish("microscope/light_sheet_microscope/UI", > json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, > indent=2)) > time.sleep(2) # wait > client.loop_stop() # stop the loop > print(device_message + ".py " + "file doesn't exist") > print("Device not added") > if __name__ == "__main__": > app = QApplication(sys.argv) > mainWin = MainWindow() > mainWin.show() > publishedMessage = mainWin.getGUIFilename() > sys.exit(app.exec_()) > > MQTT.py > import logging > from datetime import timedelta > import time > from thespian.actors import * > from transitions import Machine > import paho.mqtt.client as mqtt > import importlib > import os.path > import sys > from PyQt5.QtWidgets import * > from PyQt5.QtCore import * > from PyQt5 import QtWidgets, uic > > class device(mqtt.Client): > def on_connect(self, mqttc, obj, flags, rc): > if rc == 0: > print("Connected to broker") > else: > print("Connection failed") > > # mqttc.subscribe("microscope/light_sheet_microscope/UI") > > def on_message(self, mqttc, userdata, message): > msg = str(message.payload.decode("utf-8")) > print("message recieved= " + msg) > # print("File which you want to import(with .py extension)") > print("message topic=", message.topic) > print("message qos=", message.qos) > print("message retain flag=", message.retain) > > def run(self): > self.connect("broker.hivemq.com", 1883, 60) > > test1.py > > from PyQt5 import QtCore, QtGui, QtWidgets > import sys > from PyQt5.QtWidgets import * > from PyQt5.QtCore import * > from PyQt5 import QtWidgets, uic > from mqtt import * > > class SubWindow(QWidget): > def __init__(self, parent = None): > super(SubWindow, self).__init__(parent) > self.setMinimumSize(QSize(300, 200)) > label = QLabel("Laser", self) > > self.modeButton = QtWidgets.QPushButton("Click me",self) > self.modeButton.setGeometry(QtCore.QRect(10, 40, 81, 23)) > self.modeButton.setObjectName("Turn on") > self.modeButton.clicked.connect(self.modFun) > > def modFun(self): > print("creating new instance " + "Laser") > client = device("Laser") > client.run() > > client.loop_start() # start the loop > device_message = "ON" > time.sleep(2) > print("Subscribing to topic", > "microscope/light_sheet_microscope/UI") > client.subscribe("microscope/light_sheet_microscope/UI") > print("Publishing message to topic", > "microscope/light_sheet_microscope/UI") > client.publish("microscope/light_sheet_microscope/UI", > device_message) > time.sleep(2) # wait > client.loop_stop() # stop the loop > > def closeEvent(self, event): > self.close() > > test2.py > > from PyQt5 import QtCore, QtGui, QtWidgets > import sys > from PyQt5.QtWidgets import * > from PyQt5.QtCore import * > from PyQt5 import QtWidgets, uic > > class SubWindow(QWidget): > def __init__(self, parent = None): > super(SubWindow, self).__init__(parent) > self.setMinimumSize(QSize(300, 250)) > label = QLabel("Sub Window", self) > > self.modeButton = QtWidgets.QPushButton("Click me",self) > self.modeButton.setGeometry(QtCore.QRect(10, 40, 81, 23)) > > self.modeButton.clicked.connect(self.modFun) > > > > def modFun(self): > print("Hello there i'm Click me") > > def closeEvent(self, event): > self.close() > > Thanks.
list_of_file_name = [] my_file = getGUIFilename() list.append(my_file) Sayth -- https://mail.python.org/mailman/listinfo/python-list