How to update PyQT5 application for both Windows and macOS? - python

I have an application built with PyQT5 for both Windows and macOS. Currently, the user checks for updates by clicking the button and when there is a new update available I am redirecting them to the browser to my server to download the latest .exe (Windows) or .pkg (macOS). The issue is for say if the user downloads and installs the latest version in a different location than the previous one which will result in two instances of the same application.
I want to improve the user experience and make an auto-updater like all the established applications. When the user clicks the updates the application should download the new updates without making any hassles for the users and update the application for both the OS.
For Windows, I am using Pyinstaller to make the .exe file and then Inno Setup to make it executable. Moreover, for macOS I am using setuptools to make the .app and macOS packages app to make it executable.
It would be really great if someone could help me to implement an update feature for my PyQT5 application.

Hi there I have made a program where it can update itself, I was using tkinter, but it should work for PyQT5 if the same widgets are there. We are using GitHub for the program to download from there. Also I am on windows so I don't know if this works on mac. Here is my code:
import tkinter as tk #for you it is pyqt5
from tkinter import * #MessageBox and Button
import requests #pip install requests
import os #part of standard library
import sys #part of standard library
VERSION = 4
b1 = Button(frame, text = "Back", command = homepage)
b1.pack(ipadx= 10, ipady = 10, fill = X, expand = False, side = TOP)
checkupdate = Label(frame, text = "Looking for updates", font = ("Arial", 14))
checkupdate.pack()
try:
link = "https://raw.githubusercontent.com/SomeUser/SomeRepo/main/SomeFolder/version.txt"
check = requests.get(link)
if float(VERSION) < float(check.text):
mb1 = messagebox.askyesno('Update Available', 'There is an update available. Click yes to update.')
if mb1 is True:
filename = os.path.basename(sys.argv[0])
for file in os.listdir():
if file == filename:
pass
else:
os.remove(file)
exename = f'NameOfYourApp{float(check.text)}.exe'
code = requests.get("https://raw.githubusercontent.com/SomeUser/SomeRepo/main/SomeFolder/NewUpdate.exe", allow_redirects = True)
open(exename, 'wb').write(code.content)
root.destroy()
os.remove(sys.argv[0])
sys.exit()
elif mb1 == 'No':
pass
else:
messagebox.showinfo('Updates Not Available', 'No updates are available')
except Exception as e:
pass
Make sure you convert all the tkinter code to PyQt5 widgets. Also this is windows change the "https://raw.githubusercontent.com/SomeUser/SomeRepo/main/SomeFolder/NewUpdate.exe" to "https://raw.githubusercontent.com/SomeUser/SomeRepo/main/SomeFolder/NewUpdate.app" for mac. If this does not work comment and I will try my best to convert this to PyQt5.

Related

Python/Tkinter: running an external app with subprocess causes tkinter to become disabled

The following very simple code (Windows) plays an mp4 movie file using subprocess and the Media Player Classic portable version.
import tkinter as tk
import subprocess
root = tk.Tk()
root.geometry("800x500")
mpcpath = 'MPC-BEPortable.exe'
moviepath = "filename.mp4"
def PlayMovie():
subprocess.call([mpcpath, moviepath])
PlayButton = tk.Button(root,text="play")
PlayButton.config(command=PlayMovie)
PlayButton.place(x=50,y=50)
root.mainloop()
It works perfectly. However, while the movie is playing, Tkinter is in a disabled state. That is, if you try to interact with the Tkinter app, Windows sends an error saying that the app is not responding. Is there a way (using subprocess or some other method) to have the media player go off and run in some other thread, and leave the Tkinter application operational?

tkinter exe added to startup shows console window

I made a python application in python.
Swaminarayan.py
import tkinter as Tkinter
def main():
top = Tkinter.Tk()
top.geometry("500x500")
B = Tkinter.Button(top, text ="Ghanshyam Maharaj")
B.pack()
top.mainloop()
if __name__ == '__main__':
main()
I converted this code to .exe using cx_freeze in virtual environment
setup.py
import sys
from cx_Freeze import setup, Executable
build_exe_options = {"packages": ["os"], "excludes": []}
base = None
if sys.platform == "win32":
base = "Win32GUI"
setup(
name = "Swaminarayan",
url='https://github.com/ajinzrathod/Swaminarayan',
version = "0.1",
description = "My GUI application!",
options = {
"build_exe": build_exe_options,
"bdist_msi": {
'install_icon': r"F:\executable-python-windows\Swaminarayan\icon.ico",
}
},
executables = [
Executable("Swaminarayan.py",
base=base,
icon=r"path\to\icon.ico",
shortcutName='Swaminarayan',
shortcutDir='DesktopFolder',
)
]
)
and then created MS Installer
py setup.py bdist_msi
This creates an installer and when I installed Swaminarayan.exe, it opens normally without a console window.
I wanted my Swaminarayan Application to start at boot.
So I created a batch file in C:\Users\Ajinkya Rathod\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup named open.bat
And pasted the following code
"C:\Users\Ajinkya Rathod\AppData\Local\Programs\Swaminarayan\Swaminarayan.exe"
Which is the path to my installed Swaminarayan.exe
It works perfectly till here.
The main problem is here. When I double-click on the batch file, it also loads a console window. I just want the GUI created with Tkinter. But it also loads the console window.
I can't find the source but some StackOverflow answers asked to used start in a batch script like this
start "C:\Users\Ajinkya Rathod\AppData\Local\Programs\Swaminarayan\Swaminarayan.exe"
But the problem is that including start in batch scripts removes the console window along with the GUI application. I don't want my GUI to vanish. I just want to get rid of Console Window.
Trying this since long time. Any help would be appreciable.
According to Microsoft's documentaiton, start's "title" is optional, but depending on the options used, it can become an issue, like you are experiencing here. Currently your script is starting cmd with a title of the script path.
So include the empty title ""
start "" "%localappdata%\Programs\Swaminarayan\Swaminarayan.exe"

Main Window Icon Not Displayed when Frozen with cx_Freeze

I want to display a custom icon in a PyQt window after freezing the baseline with cx_Freeze. The icon displays fine when the unfrozen script is executed from within the IDE (Spyder, for me). I'm using PyQt5, Python 3.6, and Windows 10. Here is my Python script (IconTest.py) that creates a main window and shows the path to the icon and whether the path exists. The icon file needs to be in the same directory as IconTest.py:
import sys, os
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QIcon
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(200, 300, 600, 100)
if getattr(sys, 'frozen', False): #If frozen with cx_Freeze
self.homePath = os.path.dirname(sys.executable)
else: # Otherwise, if running as a script (e.g., within Spyder)
self.homePath = os.path.dirname(__file__)
self.iconFileName = os.path.join(self.homePath, 'myIcon.ico')
self.setWindowIcon(QIcon(self.iconFileName))
self.setWindowTitle('Icon')
self.label1 = QLabel(self)
self.label2 = QLabel(self)
self.label1.move(10, 20)
self.label2.move(10, 40)
self.label1.setText("Path to icon file: " + str(self.iconFileName))
self.label2.setText("Does file exit? " + str(os.path.exists(self.iconFileName)))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
Here is my result when running the script from within Spyder (unfrozen). As you can see, there is an icon displayed that resembles a stopwatch:
Here is my setup.py for creating the frozen baseline:
from cx_Freeze import setup, Executable
import os, sys
exeDir = os.path.dirname(sys.executable)
platformsPath = os.path.join(exeDir, "Library\\Plugins\\Platforms\\")
iconPath = os.path.join(os.path.dirname(__file__), "myIcon.ico")
exe=Executable(script="IconTest.py", base = "Win32GUI", icon = iconPath)
includes=[iconPath, platformsPath]
excludes=[]
packages=[]
setup(
version = "0.1",
description = "My Icon Demo",
options = {'build_exe': {'excludes':excludes,'packages':packages,'include_files':includes}},
executables = [exe]
)
Here is my result when running the frozen script (the executable in the build directory). As you can see, the stopwatch icon is replaced with a generic windows icon:
Suggestions?
Interesting question and nice minimal example. After some searching I guess it could have to do with PyQt5 missing a plugin/DLL to display .ico image files in the frozen application. See e.g. How to load .ico files in PyQt4 from network.
If this is true, you have 2 options:
Try the same example with a .png file as window icon
If the plugins directory is included in the frozen application but it cannot find it, try to add the following statements
pyqt_dir = os.path.dirname(PyQt5.__file__)
QApplication.addLibraryPath(os.path.join(pyqt_dir, "plugins"))`
before
app = QApplication(sys.argv)
in your main script. See this answer.
If the plugins directory is not included in the frozen application, you need to tell cx_Freeze to include it using the include_files entry of the build_exe option. Either you manage to dynamically let your setup script include it at the place where PyQt5 is looking for it, using a tuple (source_path_to_plugins, destination_path_to_plugins) in include_files, or you tell PyQt5 where to look for it, using QApplication.addLibraryPath.
In your previous question to this issue you actually had an entry to include a Plugins\\Platforms directory in your setup script, maybe you simply need to repair this include. Please note that cx_Freeze version 5.1.1 (current) and 5.1.0 move all packages into a lib subdirectory of the build directory, in contrary to the other versions.
ANSWER: I have been using the Anaconda platform and read in other posts that there are issues between PyInstaller and Anaconda because of the way Anaconda structures its content. Thinking the same issue might exist with cx_Freeze, I installed Python (no Anaconda) on a different machine and froze the script from this new Python installation.
The icon appeared as expected in the frozen script. To make the icon display properly, I made the following changes to the setup.py script:
Removed import sys
Removed the line exeDir = ...
Removed the line platformsPath = ...
Removed platformsPath from the includes = list
I had a similar problem and posted an answer here.
Basically, I have overcome this issue by storing the file in a python byte array and loading it via QPixmap into a QIcon.

Python Pyinstaller exe opens multiple instance of tkinter window, Python Firebase [Video]

I have a scraping script of where im using tkinter for ui. When i build the exe(with pyinstaller) and open it it working well, But When i close it, it opens multiple instance of tkinter Window. I cant paste the full code. So i pasted all the tkinter code i am using.
Here is the Full code Github Gist here
import requests
from lxml import html
from tkinter import *
import tkinter as ttk
import re
import datetime
import os
from firebase import firebase
import hashlib
#import App as App
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
#Region Tk
root = Tk()
root['height'] = 400
root['width'] = 600
global firebase
firebase = firebase.FirebaseApplication('#######URL####',None)
f1 = Frame(root)
f1["height"] = root["height"]
f1["width"] = root["width"]
root.title("JD Scraper - Gear Up Studio ")
Label(f1,text = "Input Url : Example : https://www.justdial.com/Ahmedabad/Gyms ").grid(row=0,column = 0,)
def getBool(event):
print(boolvar.get())
#Check Button
global boolvar
boolvar = BooleanVar()
boolvar.set(False)
boolvar.trace('w', lambda *_: print("The value was changed"))
cb = Checkbutton(f1, text = "Tele Phone number", variable = boolvar)
cb.bind("<Button-1>", getBool)
cb.grid(row=1, column=1)
global key_filled
key_filled = Entry(f1,width=50)
key_filled.grid(row=2,column=0)
key_filled.focus_set()
global activate_button
activate_button = Button(f1 , text="Active Now")
activate_button.bind("<Button-1>",activate_key)
activate_button.grid(row=2, column=1)
result = Label(f1, width=50)
result.grid(row=1,column=2)
global submit_button
submit_button = Button(f1 , text="Scrape Now")
submit_button.bind("<Button-1>",button_clicked)
submit_button.grid(row=1, column=0)
submit_button.config(state=NORMAL)
key_validation()
f1.pack()
root.mainloop()
Demo Video here
i was facing the exact the same problem with firebase and pyqt5. After many tries i examine the firebase library. In init there is close_process_pool() function which is called when program exits and close all the multiprocessing pools. In tkinter and pyqt case we dont need this function as all the process is the childs of main GUI thread so simple removing that function resolves the problem.
Change the init file to this will resolve the issue.
import atexit
#from .async import process_pool
from firebase import *
'''
#atexit.register
def close_process_pool():
"""
Clean up function that closes and terminates the process pool
defined in the ``async`` file.
"""
process_pool.close()
process_pool.join()
process_pool.terminate()
'''
I can see cmd window pops when run your exe to remove that use this command -w and also to compile as onefile into dist folder use -F to compile it as a single file, do this to resolve the issue
pyinstaller -w -F replace this with your script name.py
It will compile the file as one file for you and the use will be res

Change title of Tkinter application in OS X Menu Bar

When you create an application with a GUI using Tkinter in Python, the name of your application appears as "Python" in the menu bar on OS X. How can you get it to appear as something else?
My answer is based on one buried in the middle of some forums. It was a bit difficult to find that solution, but I liked it because it allows you to distribute your application as a single cross platform script. There's no need to run it through py2app or anything similar, which would then leave you with an OS X specific package.
Anyways, I'm sharing my cleaned up version here to give it a bit more attention then it was getting there. You'll need to install pyobjc via pip to get the Foundation module used in the code.
from sys import platform
# Check if we're on OS X, first.
if platform == 'darwin':
from Foundation import NSBundle
bundle = NSBundle.mainBundle()
if bundle:
info = bundle.localizedInfoDictionary() or bundle.infoDictionary()
if info and info['CFBundleName'] == 'Python':
info['CFBundleName'] = <Your application name here>
May not be quite what you need but I am surprised no one has mentioned the simple, platform independent way (works with Python 3.x on Win 7) :
from tkinter import Tk
root = Tk()
root.title( "Your title here" ) # or root.wm_title
and if you want to change the icon:
''' Replace the default "Tk" icon with an Application-specific icon '''
''' (that is located in the same folder as the python source code). '''
import sys
from tkinter import PhotoImage
program_directory = sys.path[ 0 ]
IconFile = os.path.join( program_directory ) + "\ApplicationIcon.gif"
IconImage = PhotoImage( file = IconFile )
root.tk.call( 'wm', 'iconphoto', root._w, IconImage )
root.mainloop()

Categories

Resources