PyQt API for detecting OS
I'm porting some code from C++/Qt to Python/PyQt.
What do I do with
#ifdef Q_O开发者_运维问答S_WIN
...
#else
Is there a PyQt equivalent of the Qt macro Q_OS_WIN?
Why you ever want to do this instead of using Python sys.platform
from sys module?
import sys
if sys.platform == 'win32':
print("win")
else:
print("winner!")
I have to mention that win32
is the same even if you run on x64 python.
Unlike C++, there is no preprocessor in Python. This means that you can't completely exclude blocks of code from being seen by the compiler/interpreter.
For most practical purposes, however, it is sufficient to place the relevant block of code inside a Python if
statement.
Take a look at Python: What OS am I running on?
To test if the underlying operating system is a macintosh
import PyQt4.QtGui
MAC = hasattr(PyQt4.QtGui, "qt_mac_set_native_menubar")
To test if the underlying operating system is Linux/X11, BSD, Solaris
import PyQt4.QtGui
X11 = hasattr(PyQt4.QtGui, "qt_x11_wait_for_window_manager")
From Mark Summerfield's book
I don't know of a specific Windows test but if the two above returned None then it's likely the system is windows.
精彩评论