This question already has answers here:
PyQt - how to detect and close UI if it's already running?
(3 answers)
Closed 7 years ago.
I have created a pyqt4 app and I want to make it so only one instance (of QApplication) is allowed to run.
The program reads and writes audio files, and if more than 1 instance is running, Windows (linux is fine) throws errors that 2 programs are trying to access the same files. I see a lot of java and C apps that will display a simple dialog if the program is already running, I just want to know how to do this in pyqt4.
A little help?
This kind of programming pattern is called a "singleton" instance or a "singleton application".
Usually it is done with a global mutex or by locking a file early in the life of the program.
And when you program launches, if the file handle is already locked, then you exit.
Qt Solutions has it here: http://doc.qt.digia.com/solutions/4/qtsingleapplication/qtsingleapplication.html
https://qt.gitorious.org/qt-solutions/qt-solutions/source/841982ceec9d30a7ab7324979a0fd5c9c36fd121:qtsingleapplication
It would probably take a bit of work to get those global mutexes/locks to work in pyqt, since pyqt doesn't have the qt-solutions part in it yet as far as I could tell.
Here is an alternative that uses a cross platform python script:
Python: single instance of program
Hope that helps.
Thanks. I Used https://gitorious.org/qsingleapplication/qsingleapplication/source/ca13324b0f5bdfcaf4e379a78108f0bd85fed98a:qSingleApplication.py#L66 And Called QSingleApplication On My MainWindow And Works Fine
Related
This question already has an answer here:
Passing extra arguments through connect
(1 answer)
Closed 2 years ago.
so I'm currently working on a PyQt5 GUI, and as always need to connect some signals to method calls.
Naturally I've looked up a standard syntax to do so and used it throughout my entire project (it's been working so far with more then 20 different signals)
That syntax is: self.widget.signal.connect(lambda x: whatever)
So I recently got to the point of connecting the QPlainTextEdit signal "textChanged()" to one of my methods and it just didn't work. I've tried replacing my method with a simple print(text) but that didn't help. My next step was testing wether another signal of the same widget worked and it did!
So now I have the following code:
self.plainTextEdit.textChanged.connect(lambda x: print("testTextChanged"))
self.plainTextEdit.blockCountChanged.connect(lambda x: print("blockCountChanged"))
and the upper signal doesn't trigger, but the lower one does.
I've already read the documentation of QPlainTextEdit, textChanged() should be a valid signal of this class. I've also used the same signal on several QLineEdits within my project.
Does anyone have any suspicion as to why this behaviour is occuring? Maybe I did make an error that I just can't recognize. (I'm trying to trigger the signal by simply typing into the textBox on the GUI, whereas blockCountChanged get's triggered whenever I'm pressing enter while editing it)
So, the comment of musicamente (comment on the question) did answer it. The reason it did not work is, because the textChanged signal of QPlainTextEdit does not have any parameters (QLineEdit textChanged does f.e.). That's why the lambda should not have and parameters -> the correct code should be:
self.plainTextEdit.textChanged.connect(lambda: print("testTextChanged"))
PS: just answering this if someone searches for the same stuff.
This question already has an answer here:
python IDLE shell appears not to handle some escapes correctly
(1 answer)
Closed 3 years ago.
I'm a Raspberry Pi using Thonny Python's IDE to code, and whenever I try to use a backspace in the form of '\b', it outputs as a check mark:
Like this one
I've already looked at some articles on here and only a few mention a GUI bug within Tkinter, but since I'm not using that I'm not sure what's going on. I've also tested it with a simple print('a\b') command in the IDLE, but it still comes out with the check mark following the a.
For reference, I'm using code from this answer: https://stackoverflow.com/a/39504463/11357553
It's specifically this part that produces the oddity:
sys.stdout.write('\b')
sys.stdout.flush()
I'm simply looking to complete the provided function by deleting the most recent character printed to the spinning cursor. I'm rather new and have been able to solve most of my problems, but this one leaves me stuck.
IDLE does not implement a terminal. It does not process either control characters or escape sequences. Run your program in an actual terminal emulator and not via IDLE.
IDLE uses tkinter to implement its GUI. It uses two TK text areas to form the editor and console parts of its windows.
This question already has answers here:
How do I handle the window close event in Tkinter?
(11 answers)
Closed 4 years ago.
I am building a GUI with Tkinter at the moment. With the GUI I can create files, add certain lines to the files, delete lines, save and load files. My problem is, that i want to prevent the closing of the window if the current file wasn't saved.
Is there a way to solve this problem?
Yes. That is totally possible (unless perhaps the process is being terminated abruptly), by making use of WM_DELETE_WINDOW protocol:
...The most commonly used protocol is called WM_DELETE_WINDOW, and is used to define what happens when the user explicitly closes a window
using the window manager.
This question already has an answer here:
PyQt : Linking buttons to functions in my program
(1 answer)
Closed 6 years ago.
So I have this GUI that has 2 empty text spots to fill and a "Run" button
I want to make it that the 2 empty text spots go to values in the program and the run button will run the python "Main" program ..
How can i do that ?
You need to have a look at the documentation for PyQt signals and slots. Normally, when you install PyQt you also install some great examples. Walk through the code and it will teach you a lot (search your filesystem for "qtdemo" or standarddialogs.py).
New-style signals and slots
Old-style signals and slots
Here's a snippet from standarddialogs.py:
self.saveFileNameButton = QPushButton("QFileDialog.get&SaveFileName()")
...
self.saveFileNameButton.clicked.connect(self.setSaveFileName)
...
def setSaveFileName(self):
This is complex to explain, I hope this will not end up being a vague question getting vague answers.
If this is not the right place to ask this, you may help me to find the proper one.
I have a plugin for Photoshop based on the Listener, so it captures any input from the user.
The plugin creates a python module (called here "ps") containing basically the hInstance and the hwnd of the photoshop window.
Then this plugin, using plain python commands in the plugin for the module like those
PyRun_SimpleString("import Photoshop");
PyRun_SimpleString("Photoshop.showTools()");
will load a special module (here called "Photoshop") that will initialize pyqt and using the QtWinMigrate and the ps module to get the hInstance like this: QMfcApp.pluginInstance(ps.GetPluginInstance()), will start pyqt in photoshop. Here an example code of the Photoshop module using the ps module:
from PyQt4.QtWinMigrate import QMfcApp
from PyQt4.QtGui import QPushButton
import ps #this is implemented in the photoshop plugin (based on the Listener plugin)
#create the plugin instance here
app=QMfcApp.pluginInstance(ps.GetPluginInstance())
def showTools():
box = QPushButton()
box.show()
app.exec_()
Again then, the sequence is like this:
When the plugin starts in photoshop "ps" module is created, then it will load the "Photoshop" module that will load and bind properly pyqt. In the "Photoshop" module I can load any python module, widgets are properly working and everything works really well inside Photoshop.
But now the problem is: using Wacom tablets in Photoshop loose stroke sensitivity, the driver works and everything else works but the pressure sensitivity.
Apparently QMfcApp.pluginInstance will install an event filter to drive the Qt event loop while photoshop still owns the event loop. ( http://doc.qt.digia.com/solutions/4/qtwinmigrate/qmfcapp.html )
and on the paper looks fine to me.. but I could not manage to solve this by myself and I tried, more or less carefully, different approaches:
the listener plugin is not the problem. If Listener plugin runs but python is not initialized sensitivity works fine.
python itself is not a problem. If the listener starts python without gui nor pyqt, then works fine.
as soon as I call pluginInstance which should create the QApplication the issue starts and pressure is lost from the tablet. Even with the small code I wrote before.
Someone may have put pyqt as a plugin somewhere else, since the only purpose of QMfcApp is apparently this one. There is something I can configure to make it work? Is a known issue?
I would rather keep the approach (instead of connecting to photoshop externally like with COM)
I am not able to post the entire code here but let me know if you need something.. I probably can show more.
Thanks a lot for your help