Can someone tell me how do this?
I got main.py and gui.py (made in pyqt4).
The main.py runs some code, whatever, print "hello world" and I want to pass this string to gui.py in listWidget.addItem().
So in main.py I did:
from gui import Ui_Form
send_str = "hello all"
ui.listWidget.addItem(send_str)
and I get the error:
NameError: global name 'ui' is not defined.
which points to gui.py as in gui.py is
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
If i put both code in one py, all if working.
How to separate these two, since i want my main.py to execute in endless loop, and from time to time i want to open gui and check whats new value main.py going to send to window?
First problem is that ui variable is local to the if name == "main"
Second problem is the if itself because that branch will not be activated when you import a module, that is similar to the main() in c
3rd problem: the gui in QT has to run in the main thread. This means that your algorithm needs to run in another thread (there is an alternative with timers but it is ugly and harder to implement)
See a tutorial on QT threads and use the signal-slot mechanism to comunitate between gui thread and algorithm thread.
Related
I have a pyqt5 application. In the program you can open various other windows from the MainWindow. The different windows are stored in a separate module and are imported into the main file, which represents the MainWindow. Furthermore I use several custom widgets, which are also stored in a separate module and imported into the main file.
The program runs smoothly, but it takes a few seconds for the program to start. Exactly I can not say what causes the delay at startup. However, it seems to be due to my custom modules.
Is there a possibility to speed up the loading of the modules or to load the modules after the MainWindow is open?
The main file looks something like his:
#other imports
...
#import custom modules
from MyWidgets import MyTreeView
from MyWindows import MySecondWindow
basedir = Path(__file__).parent.absolute()
class App(QMainWindow):
def __init__(self):
super(App, self).__init__()
uic.loadUi(os.path.join(basedir, "gui", "MainWindow.ui"), self)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = App()
window.showMaximized()
window.show()
sys.exit(app.exec_())
I am trying to create a GUI using PyQt5. I have an external script with functions, to change values on the screen. However I cannot access the attributes of the Qt object externally.
In my external script I have tried:
Main.MainWindow.label_55...
Main.ui.label_55...
And multiple other variations
*Main is the name of the PyQT5.py file
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_())
Error:
AttributeError: module 'Main' has no attribute 'MainWindow'
From your question I suppose by "accessing externally" you mean "by importing". If you import a module with an "import protection" statement like
if __name__ == "__main__":
the code inside this block does not run. It only runs when you execute the module directly, not via an import. That's why the other module does not see MainWindow.
If that's your main app window you should probably make imports the other way round. The main app imports other modules and calls them, eventually passing the main window or other objects as parameters.
my long term goal is to build a gui for an experiment in experimental physics which has a continuously running gui. By pushing a button I would like to be able to run a pyhton script of my choice which can interact with the running gui. For example setting a number to a spin box.
I attached a starting project. A spinbox and a button. If the button is pressed a random number is set to the spinbox and as soon as the number in the spinbox changes, it prints the number.
Is there a way to call a script (at the moment with a hard coded path) by pushing the button, which then sets the number in the gui to my choice. The content of the script (in this case the number which is set to the spin box) has to be editable during the runtime of the gui.
If you could provide an example for this, I would be grateful and could build the rest myself.
Thanks in advance!
import sys
import random
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import QApplication, QWidget, QDoubleSpinBox, QPushButton
class GuiInteraction(QWidget):
def __init__(self):
super().__init__()
self.initGUI()
self.CallBackFunctions()
def initGUI(self):
self.resize(400, 500)
self.move(300, 300)
self.setWindowTitle('Gui Interaction')
self.doubleSpinBox = QDoubleSpinBox(self)
self.doubleSpinBox.setGeometry(QtCore.QRect(120, 130, 120, 25))
self.doubleSpinBox.setDecimals(5)
self.doubleSpinBox.setMaximum(1000)
self.doubleSpinBox.setObjectName("doubleSpinBox")
self.pushButton = QPushButton("Run Script", self)
self.pushButton.setGeometry(QtCore.QRect(100, 300, 100, 40))
self.pushButton.setObjectName("pushButton")
def CallBackFunctions(self):
self.pushButton.clicked.connect(self.buttonClicked)
self.doubleSpinBox.valueChanged.connect(self.valueChanged)
def buttonClicked(self):
self.doubleSpinBox.setValue(random.uniform(1, 200))
def valueChanged(self):
print(self.doubleSpinBox.value())
if __name__ == '__main__':
app = QApplication(sys.argv)
MyWindow = GuiInteraction()
MyWindow.show()
sys.exit(app.exec_())
I'm thinking you can call a FileDialog, pick a script and use:
mod = __import__(path)
, and than should the script be adequately built with a "run" function of some kind you can just launch it by:
mod.run()
Check this question also.
One way would be to just communicate with an external script through stdin/stdout
my_script.py
def main():
print '4.2'
if __name__ == '__main__':
main()
gui_script.py
import subprocess
...
def buttonClicked(self):
try:
value = float(subprocess.check_output(['python', 'my_script.py']).strip())
except ValueError:
value = 1.0
self.doubleSpinBox.setValue(value)
If you need to pass arguments to your function you can just pass them as additional arguments to the subprocess.check_output call, and them read them from sys.argv (they'll all come in as strings, so you'd have to convert the types if necessary, or use a library like argparse, which can do the type-casting for you) in the called script.
I went for the solution of #armatita, even though I changed it a little. After a brief research __import__ seems to be replaced by the libimport libary, which I now use. The following lines have been added:
Header:
import importlib
import threading
and in the main function:
def buttonClicked(self):
ScriptControlModule = importlib.import_module("ExternalScript")
ScriptControlModule = importlib.reload(ScriptControlModule)
ScriptControlThread = threading.Thread(target=ScriptControlModule.execute,args=(self,))
ScriptControlThread.start()
My question is answered by the importlib lines. I also wanted to start the script in a subthread, so in the case it crashes due to a typo or anything else, the whole gui does not follow the crash.
The ExternalScript is in the same folder and named ExternalScript.py
import random
def execute(self):
self.doubleSpinBox.setValue(random.uniform(1, 5))
Three simple lines of code. I can change these lines while running and get different values in the SpinBox. Works out perfectly!
I did a small script on python to do some stuff, and I want to ask user input first. This is my current code:
import sys
from PySide import QtGui
app = QtGui.QApplication(sys.argv)
gui = QtGui.QWidget()
text, ok = QtGui.QInputDialog.getText(gui, "question",
"""please put the thing I need from you""")
print(text, ok)
if ok:
app.exit()
else:
app.exit()
app.exec_()
print ("I'm aliveeeee'")
The dialog pop-ups exactly as I want, but app.exec_() never ends so the rest of the code is never executed (and the process never finish) I tried to kill it with app.exit(), app.quit(), I also try to show() and close() the QWidget, but nothing is working.
If I do gui.show() before calling the QInputDialog and then close the widget manually, the app closes successfully. However, this is not the behavior I want.
Can you guide me on which is the best way to close the exec loop after I got my data?
PD: This is going to be a windows app (with py2exe) and using the shell is not an option.
Just don't call app.exec_()
The problem here is that this is a toy example. In real life, usually you will show some UI and then call app.exec() to let the user interact with it.
I am experiencing different behaviour on the same code using the python console and a python script.
The code is as follows:
import gtk
import webkit
win = gtk.Window()
win.show()
web = webkit.WebView()
win.add(web)
web.show()
web.open("http://www.google.com")
When running the code in the python console, the output is a new frame that contains the google main page.
When running the code as a script, the result is a void frame. It closes very fast but even if I use a delay function, the webkit is not added to the frame.
How is it possible?
Furthermore, using PyDev IDE it flags: "unresolved import: gtk",
but if i run the project, the program starts without problem of compilation. is it normal?
Add
gtk.main()
to the end of your script. This starts the gtk event loop.
import gtk
import webkit
class App(object):
def __init__(self):
win = gtk.Window()
win.connect("destroy", self.destroy)
web = webkit.WebView()
web.open("http://www.google.com")
win.add(web)
web.show()
win.show()
def destroy(self, widget, data = None):
gtk.main_quit()
app = App()
gtk.main()
My guess is that the console keeps the python session open, while at the end of the script the program closes. When the script closes, it takes everything it created with it.
Something to test this theory: if you type "exit" in the console do you see the interface shut down in the same manner? If so, think of some code (e.g. a pause like a raw_input) that will allow the script to stay open.
Good luck!