PySyde2 / fbs freeze / fail to execute script main - python

Hope you doing well guys! I am starting a project using PyCharm and also a Virtual Env. Can someone help please?
I have these file:
main.py with the code:
from fbs_runtime.application_context.PySide2 import ApplicationContext
import sys
from package.main_window import MainWindow
if __name__ == '__main__':
appctxt = ApplicationContext() # 1. Instantiate ApplicationContext
window = MainWindow()
window.resize(250, 150)
window.show()
exit_code = appctxt.app.exec_() # 2. Invoke appctxt.app.exec_()
sys.exit(exit_code)
I have another file main_window.py with these code:
from PySide2 import QtWidgets
class MainWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setup_ui()
def setup_ui(self):
self.create_widgets()
self.modify_widgets()
self.create_layouts()
self.add_widgets_to_layouts()
self.setup_connections()
def create_widgets(self):
self.btn_click = QtWidgets.QPushButton("Click")
def modify_widgets(self):
pass
def create_layouts(self):
self.main_layout = QtWidgets.QVBoxLayout(self)
def add_widgets_to_layouts(self):
self.main_layout.addWidget(self.btn_click)
def setup_connections(self):
self.btn_click.clicked.connect(self.bouton_clicked)
def bouton_clicked(self):
message_box = QtWidgets.QMessageBox()
message_box.setWindowTitle("Bravo")
message_box.setText("Première application réussi")
message_box.exec_()
Finally I have create a freeze.sh file with this:
source ~/PycharmProjects/venv/Scripts/activate
cd ~/PycharmProjects/echaufement/
fbs clean
fbs freeze
I cd my src/main folder and usig: sh freeze.sh
it create a target folder containing my App.exe
but when open my App.exe I have the following error: fail to execute script main
Finally after debugging -fbs freeze --debug- and I have this - see image please
Thank you for your help.

Is there some pathing issue with ./Scripts/fbs-script.py line 11?
Also as a general rule, fbs doesn't play well beyond python 3.6. Try freezing the app again when a python 3.6.x venv?
I've outlined how to use a python 3.6.x virtualenv to build fbs app here: How to compile PyQt5 program that uses python 3.8

Related

pyqt5 default native menu text on MacOS

Is there a way to change default menu title for native menu on MacOS for a simple pyqt5 app?
I'm trying to change "Python" menu entry on above image. Is there a way to rename it? Can I hide it somehow?
This code prints only "File":
menubar = self.menuBar()
for item in menubar.actions():
print(item.text())
menubar.setNativeMenuBar(False) doesn't help too (just move "File" into the MainWindow).
Update Sample app code:
from PyQt5 import QtCore, QtWidgets, uic
import sys
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
# QtCore.QCoreApplication.setApplicationName('QtFoo') # doesn't work
uic.loadUi('App.ui', self)
self.show()
# app = QtWidgets.QApplication(sys.argv)
app = QtWidgets.QApplication(["MyCoolApp"])
# app.setApplicationName("QtFoo") # doesn't work
# app.setApplicationDisplayName("Display Name")
window = Ui()
app.exec()
If you are not willing to package your app, you could create a temporary symbolic link to python as described here. This link can be used to execute python apps while displaying a custom name in the title bar:
go to the location of your app (e.g. my_app.py) file in the terminal
create symbolic link (replace location to your python interpreter, replace "BeautifulNaming" with desired name)
ln -s /Users/Christian/miniconda3/bin/python BeautifulNaming
call link to python with your script
./BeautifulNaming my_app.py
The "python" in the system menu bar appears because you run the script from the python, as soon as you package the application the title will disappear. For example the following code
# FileName PyQt5MenuProblem.py
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
class AppTest(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("My application")
self._createMenuBar()
def _createMenuBar(self):
self.menuBar = self.menuBar()
self.menuBar.setNativeMenuBar(False)
fileMenu = self.menuBar.addMenu("&File")
editMenu = self.menuBar.addMenu("&Edit")
if __name__== "__main__":
app = QApplication(sys.argv)
plotWindow = AppTest()
plotWindow.showMaximized()
sys.exit(app.exec_())
after packaging with
pyinstaller --onefile PyQt5MenuProblem.py
looks like
Key words: MacOS, User Interface, LSUIElement, pyinstaller, pyqt5

Loading QDesigner UI file from Python script using PyQt5

I tried to load Ui file from Python script as follow.
from PyQt5 import uic, QtWidgets
import sys
class Ui(QtWidgets.QDialog):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi('SomeUi.ui', self)
self.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Ui()
sys.ex
When I debug and reach to the line window = Ui(), I have error as
-> window = Ui()
(Pdb) n
TypeError: TypeErro...ndow')),)
What is wrong with my code?
You can compile .ui file into .py file by pyuic utility, which is part of PyQt.
Find the utility somewhere in python dist-packages, for example, on my system (debian linux), it's placed in
/usr/lib/python3/dist-packages/PyQt5/uic/pyuic.py
Run it with python, specifying source .ui file and -o parameter with result py file.
cd /usr/lib/python3/dist-packages/PyQt5/uic/
python3 ./pyuic.py /home/user/my-app/my-file.ui -o /home/user/my-app/my-file.py
You'll get .py file, that would contain something like this:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(555, 470)
// and so on... lots of lines...
Import this file, and subclass this Ui_MainWindow (the name can be different) by your window class.

pyqt code do not show any output in python spyder

I try to build an interface. I installed PyQt 5 (5.6.0) in Anaconda 3.6 (Python 3.6.3). I use Spyder (3.2.6) as my compiler.
import sys
from PyQt5 import QtGui,QtCore
from PyQt5.QtWidgets import QPushButton,QApplication,QMainWindow
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(50,50,500,300)
self.setWindowTitle("PyQt!")
self.show()
if __name__ == "__main__":
def run():
App=QApplication(sys.argv)
Window()
App.exec()
run()
There is no error while running this command. However, there is no output too. Shouldn't I see a window of 500 pixels x 300 pixels at (50, 50)?
Thanks for your insights :)

Context menu disappears if PySide app is running from IPython interactive console

In the minimal example given below, the context menu (right click on white section of the gui) is displayed only briefly and then disappears. This is the case if the app is started from the IPython (0.13.1) console. When started normally from shell it works as it should.
import sys
from PySide import QtGui, QtCore
from IPython.lib.guisupport import get_app_qt4, start_event_loop_qt4
class ContextTestGui(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_ContextTestWindow()
self.ui.setupUi(self)
self.ui.treeView.addAction(self.ui.actionCopy)
self.ui.treeView.addAction(self.ui.actionShow)
class Ui_ContextTestWindow(object):
def setupUi(self, ContextTestWindow):
ContextTestWindow.resize(200, 100)
self.treeView = QtGui.QTreeView(ContextTestWindow)
self.treeView.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
self.treeView.setMinimumSize(QtCore.QSize(100, 100))
self.actionCopy = QtGui.QAction("Copy",ContextTestWindow)
self.actionShow = QtGui.QAction("Show",ContextTestWindow)
def create_window(window_class,**kwargs):
app = get_app_qt4(sys.argv)
window = window_class()
window.show()
start_event_loop_qt4(app)
return window
if __name__ == '__main__':
simgui = create_window(ContextTestGui)
I believe this is IPython Issue #2380, which should be fixed in current git master.
If you want your app to workaround this bug in 0.13, you will have to ship with your own code a version of IPython/lib/inputhookqt4.py from master, and monkeypatch IPython with:
from IPython.lib import inputhook
inputhook.enable_qt4 = my_enable_qt4
OR, alternatively, just override create_inputhook_qt4
from IPython.lib import inputhookqt4
inputhookqt4.create_inputhook_qt4 = my_create_inputhook_qt4
I expect both approaches should work, as long as you do it before %gui qt is called.
How do you run the application ? In[1]: %run file.py?
I can't reproduce it on ~last master (feb 4 f46bfec 08389b4) with OS X

Python PyQt how to set environment variable for QProcess?

I'm trying to set an environment variable in a QT app for a QProcess that's being run. The code is below. The environment variable does not appear to be set however when inside the test. Any suggestions?
def runUbootTests(self):
env = QtCore.QProcessEnvironment.systemEnvironment()
env.insert("LINUX_ETH_ADDR", "3c:98:bf:00:00:f4")
self.process.setProcessEnvironment(env)
self.process.readyReadStandardOutput.connect(self.readReady)
self.process.start("make", ("clean", "check_uboot"))
Have you tried using http://docs.python.org/library/os.html#os.environ? This modified the environment for the current process (as can be seen in /proc as well).
This new environment should be passed along to any spawned processes as well.
The code you posted does not seem obviously wrong, and works for me.
Here's my test files and output:
Makefile:
clean:
#echo 'SHELL:' $(SHELL)
check_uboot:
#echo 'ADDR:' $(LINUX_ETH_ADDR)
test.py:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.button = QtGui.QPushButton('Test', self)
self.button.clicked.connect(self.handleButton)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
self.process = QtCore.QProcess(self)
def handleButton(self):
env = QtCore.QProcessEnvironment.systemEnvironment()
env.insert("LINUX_ETH_ADDR", "3c:98:bf:00:00:f4")
self.process.setProcessEnvironment(env)
self.process.readyReadStandardOutput.connect(self.readReady)
self.process.start("make", ("clean", "check_uboot"))
def readReady(self):
print str(self.process.readAllStandardOutput())
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
output:
$ python2 test.py
SHELL: /bin/sh
ADDR: 3c:98:bf:00:00:f4

Categories

Resources