Unable to run Tkinter and Flask at the same time - multithreading

I have the demo.py code where I let's say open the Web-Cam in tkinter window. Now, I also have flask code to display hello #localhost.
When I run the demo.py file it runs the function that I write first For ex:
import libraries
Flask code
Tkinter code
if __name__ == '__main__':
window.mainloop() #for tkinter This will run first
app.run() #for flask This will run when I close the Tkinter windows
How can I run both at the same time ?

Related

How to change the source of python used by atom runner or script in Atom?

I want to run python code with Kivy by atom-runner or script, but when I run the simple any codes with Kivy, I got ModuleNotFoundError. I checked python path by
import sys
print(sys.prefix)
which prints ''/Library/Frameworks/Python.framework/Versions/3.7''
I guess I need to use Anaconda's python because I can run Kivy app on terminal and terminal uses python3.7 showing Anaconda.inc things.
But I can not find where to write path of python in Atom.
I tried pip install Kivy and it worked but nothing changed in Atom. And Kivy3 is in Application folder.
Just in case, my Kivy codes is this
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
class IntroKivy(App):
def build(self):
return Button(text="Hello, World!")
if __name__ == "__main__":
IntroKivy().run()
It will be perfect if I can run this codes without using terminal as it is faster and quick.
Best

PyQt5 howto load UI file in bundle

I'm trying to load a Qt5 file in a bundle on a Mac.
#!/usr/bin/env python
from PyQt5.QtWidgets import (QApplication, QMainWindow)
from PyQt5 import uic
import sys
import os
MainUI = os.path.dirname(os.path.realpath(__file__)) + "/data/MainUI.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(MainUI)
class MyForm(QMainWindow,Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyForm()
w.show()
sys.exit(app.exec_())
This works fine in the commandline but when I bundle this with pyinstaller the program aborts because it can't find the file mainUI.ui. I found this solution but I can't get it working (resource_path function not found).
I've made another and that worked fine but this one (a one file program) I can't get working.
Edit:
I stopped trying to fix this and went for the multiple file solution: 1 file to startup and one for the PyQt functions. That works 100%.

my GUI doesn't run as executable using pyinstaller?

I've used pyinstaller to convert my Tkinter GUI to an executable file. When I run the exe through command prompt, it reads it but does nothing (I know it reads it because when it has errors they are shown). I'm very new to programming so I wrote a simpler script to generate a window only, converted this to exe and encountered the same problem. Any fundamental rules I'm not aware of that will correct this? Here's the simple code:
import tkinter
from tkinter import *
import os
import sys
def startup():
global CODES
CODES = Tk()
CODES.title("TEST WINDOW - PYTHON EXECUTABLE")
CODES.geometry("1555x569+20+20")
return CODES.mainloop
startup()
Saved it as "test.py", converted to exe by "pyinstaller test.py -F" in Command Prompt. When run, it just pauses then returns to Command Prompt

.show() and/or.exec() from Pyqt5 not working in Spyder

I have Python 3.6.3 and Spyder 3.2.4, both installed along with Pyqt5 yesterday on a computer with a new installation of Windows 10. I am having a problem running some previously written Pyqt5 code in Spyder. The below code shows a minimum code snippet that reproduces the problem.
The Spyder IPython console hangs indefinitely on running the code, and never opens the window. Ctrl-C does not stop execution, so I have to kill Spyder. If I try to run it line by line, I observe that "w.show()" executes but does nothing, while "app.exec()" is the line that hangs. In contrast, if I instead run it from the command line console, "w.show()" makes a nonfunctional window appear, while "app.exec()" makes the window functional. All of the non-pyqt code I have run in Spyder executes just fine.
I'd prefer to develop code in Spyder because running code from the command line often takes 30-60s to start (presumably from the imports.)
I could run the code in Spyder on my old (now broken) system, so clearly it is a problem with my installation or computer. But I am at a loss as to what. I have already tried disabling my virus software, updating all of the relevant packages with pip, resetting Spyder from the Anaconda prompt, and restarting the computer. Any other ideas?
import sys
from PyQt5.QtWidgets import QApplication, QWidget
if not QApplication.instance():
app = QApplication(sys.argv)
else:
app = QApplication.instance()
print("app started")
w = QWidget()
w.resize(250, 250)
w.move(200, 200)
w.setWindowTitle('Test window')
print('app setting')
w.show()
print("shown")
app.exec_()
#sys.exit(app.exec_())
print("exit")
When running this in python it needs to be modified as below. I've modified your script a little to be able to let it run for you some of the main PyQt conventions. See also my inline comments to make sense of what you're trying to accomplish with your print lines. Enjoy!
# -*- coding: utf-8 -*-
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget
class MyMainUI(QtWidgets.QMainWindow):
def __init__(self, parent=None):
print ' Start Main'
super(MyMainUI, self).__init__(parent)
self.setWindowTitle('Test window')
self.resize(250, 250)
self.move(200, 200)
MySpidercodeHook()
def MySpiderCodeHook(self):
...link/run code...
if __name__ == '__main__':
app = QApplication(sys.argv) # no QApplication.instance() > returns -1 or error.
# Your setting up something that cannot yet be peeked
# into at this stage.
print("app started")
w = MyMainUI() # address your main GUI application
# not solely a widgetname..that is confusing...
x = 200
y = 200
# w.move(x, y) # normally this setting is done within the mainUI.
# See also PyQT conventions.
print 'app setting: moving window to position : %s' % ([x, y])
w.show()
print("shown")
app.exec_()
#sys.exit(app.exec_()) # if active then print "exit" is not shown..
# now is while using app.exec_()
#sys.exit(-1) # will returns you '-1' value as handle. Can be any integer.
print("exit")
sys.stdout.flush() # line used to print "printing text" when you're in an IDE
# like Komodo EDIT.

ValueError: character U+6573552f... Py2aap

I made a very simple program and I'm trying to export it to an app file. I'm currently using Python 3.6 and py2app to convert the py file into app.
So I created the setup file:
from setuptools import setup
OPTIONS = {'iconfile':'sc.icns',}
setup(
app = ['hello.py'],
options = {
'py2app': OPTIONS},
setup_requires = ['py2app']
)
and then in the terminal I enter:
python3 hello_setup.py py2app
After some seconds it creates the dist folder and in it there is the hello.app, the problem is when I run it, it appears a window that says "hello error" then I open the .exec file inside the application to see the terminal and it shows this error:
ValueError: character U+6573552f is not in range [U+0000; U+10ffff]
Why does it appear? How do I fix it? Thank you very much.
In case that it is needed, here is the code of 'hello.py'
from tkinter import *
from tkinter import messagebox
root = Tk()
def printworld():
messagebox.showinfo('Hello', 'Hello World')
button1 = Button(root, text='Press me!', command=printworld)
button1.pack()
root.mainloop()
This is an issue with the latest version of py2app==0.14.
You should open a issue with them to get it fixed in current version. In the meantime you can go back one version and it will work fine
pip install py2app==0.13

Resources