PyQt v4.8.2 + Python 3...Icons only showing on dev-machine - python

I wrote a little app with a gui to analyze xml files. I have 2 .py files where 1 is the GUI and the second handels the xml.
My Problem is that the Icons i set and also a gif that i show using QMovie are not showing up on any machine but my development machine.
The other machines do not have PyQt or Python installed. They are using my Installation-folder from Python that I've copied onto a network drive. The i just copied the python32.dll in the machines system32 folder and the app works...except for the icons.
That's how i set the icons and reference the gif:
self.setWindowIcon(QtGui.QIcon("grabb.ico"))
--------other code---------
self.movie = QMovie("load.gif", QByteArray(), self)
self.movie.setCacheMode(QMovie.CacheAll)
self.movie.setSpeed(100)
self.movie_screen.setMovie(self.movie)
self.movie.start()
The file is in the same dir as the py so this should work. I've also tried absolute paths but that didn't give me any better results.
Now i have read on stack that this could be resolved by using the ressource-system.
So i used the designer to create a ressource files and then compiled it using pyrcc4 into a .py (using the parameter for python 3). Now I'm reffering to the images like this:
self.setWindowIcon(QtGui.QIcon(":icon/grabber.ico"))
--------other code---------
self.movie = QMovie(":loader/load.gif", QByteArray(), self)
self.movie.setCacheMode(QMovie.CacheAll)
self.movie.setSpeed(100)
self.movie_screen.setMovie(self.movie)
self.movie.start()
This again works fine on my machine but nowhere else.
Any ideas as to why only ma machine shows the icons ?
I'm not using py2exe or alike!

I too came across the same situation.
One approach is to set the paths in qt.config file and place this in the location of your executable.
I tested this with Python 2.6, and its working fine without any issue.
You said that you have shared your python installation which has PyQt4 init.
Lets say you python share path is \\somesystem\Python26
Yo will find a qt.config file in \\somesystem\Python26\PyQt4. Take it and replace the below lines in qt.conf
[Paths]
Prefix = //somesystem/Python26/PyQt4
Binaries = //somesystem/Python26/PyQt4

Related

Dialog box to select option from list in Python on Windows?

I need to create a dialog box that allows user to select one option from a list in Python on Windows using PyWin32 library. PyWin32 has a DialogBox function, but I cannot find any examples how to use it and I never used it before. Could anybody give me some advice?
The window should be something similar to the one below - this has been created using Zenity (scroll bar is unneeded, this has been added by Zenity itself; I'm perfectly fine with a window that just lists the options - there will be no more than about 5-6 of them), but I would rather like to avoid using external tools like Zenity, I also cannot install other libraries on the system except PyWin32 that is already installed.
Have to reply to myself :). Within the directory where PyWin32 is installed, there is a file pythonwin\pywin\dialogs\list.py that contains a sample class ListDialog implementing exactly such a dialog. It can be either used directly "as is", with some code like following:
import pywin.dialogs.list
result=pywin.dialogs.list.SelectFromList('Select level', ['standard', 'advanced', 'expert'])
print(result)
or it can be copied to a separate file and modified to change the style/layout/behavior of the window and imported from the modified file.

Extract icon from shortcut (.lnk) file in Python?

I'm trying to extract the icons from all of the shortcuts in the Start Menu folder. So far I've managed to walk the directory tree, and I just need something to extract the icon from each shortcut. I've tried a few methods suggested across the internet, but I can't seem to make it work fully.
Method 1: Using a program called ResourcesExtract through os.system() to extract the icon from the .lnk file. I soon discovered that this doesn't work for .lnk files, only .exe or .dlls.
import os
os.system(f"resourcesextract.exe /source {shortcut}")
Method 2: Extracting the icon file from the targets of the shortcuts (which can be obtained quite easily using the pywin32 library) using ResourcesExtract. Unfortunately, this only works for some of the programs, due to some shortcuts pointing to .exes without icons.
import os
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
target = shell.CreateShortCut(shortcut).TargetPath
os.system(f"resourcesextract.exe /source {target}")
Method 3: Using pywin32 to get the icon directory. This only works for around 120 of the 300 shortcuts I need it to work on.
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
icon, status = str(shell.CreateShortCut(shortcut).IconLocation).split(",")
I also came across a way to do it using the .NET framework, but I don't know how to interface that with python or if it will even work.
Is anyone aware of a method to extract icons from .lnk files in Python that works on all shortcuts?
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
ShortCut = shell.CreateShortCut('example.lnk')
icon_location = ShortCut.IconLocation
It work for me.

Set tkinter icon on Mac OS

I am trying to change the icon that appears on my tk application for Mac OS. The last time I checked this code worked for windows. The goal is for this solution to work across all platforms.
root = tk.Tk()
app = Application(master=root)
app.master.iconbitmap("my_icon.ico")
app.mainloop()
The code is adding the default icon for a .pdf file which is not what I intended. The path to the my_icon.ico is correct. Why won't this work for Mac OS? Is there an ultimate solution that will work cross-platform?
According to the tk tcl documentation you may want to try wm iconphoto. It appears it may support OSX and it also mentions to set the file to around a 512x512 for smooth rendering in MAC.
I do not have MAC so I cannot test this but give this a shot and let me know if it helped.
Update:
As #l'L'l pointed out you may want to try root.iconphoto(True, img). I am unable to test it myself due to not having Mac.
import tkinter as tk
root = tk.Tk()
img = tk.Image("photo", file="icon.gif")
# root.iconphoto(True, img) # you may also want to try this.
root.tk.call('wm','iconphoto', root._w, img)
root.mainloop()
Here is the relevant text from the documentation here:
wm iconphoto window ?-default? image1 ?image2 ...? Sets the titlebar
icon for window based on the named photo images. If -default is
specified, this is applied to all future created toplevels as well.
The data in the images is taken as a snapshot at the time of
invocation. If the images are later changed, this is not reflected to
the titlebar icons. Multiple images are accepted to allow different
images sizes (e.g., 16x16 and 32x32) to be provided. The window
manager may scale provided icons to an appropriate size. On Windows,
the images are packed into a Windows icon structure. This will
override an ico specified to wm iconbitmap, and vice versa.
On X, the images are arranged into the _NET_WM_ICON X property, which
most modern window managers support. A wm iconbitmap may exist
simultaneously. It is recommended to use not more than 2 icons,
placing the larger icon first.
On Macintosh, the first image called is loaded into an OSX-native icon
format, and becomes the application icon in dialogs, the Dock, and
other contexts. At the script level the command will accept only the
first image passed in the parameters as support for multiple
sizes/resolutions on macOS is outside Tk's scope. Developers should
use the largest icon they can support (preferably 512 pixels) to
ensure smooth rendering on the Mac.
I did test this on windows to make sure it at least works there. I used a blue square image to test.
If the above documentations is accurate it should also work on MAC.
If you are using Mac OS you have to use a .icns image instead a .ico image.
you can use:
from tkinter import Tk
from platform import system
platformD = system()
if platformD == 'Darwin':
logo_image = 'images/logo.icns'
elif platformD == 'Windows':
logo_image = 'images/logo.ico'
else:
logo_image = 'images/logo.xbm'
root = Tk()
root.title("My App")
root.iconbitmap(logo_image)
root.resizable(0, 0)
root.mainloop()
Important Note: This method is long and a lot of work for the task at hand. However, it does come with some unrelated benefits. Note that there might be a better way, but this will work.
Anyway, moving on....
You can use py2app.
Py2app will turn your program into a .app, meaning it runs as an application (because it is). When using tkinter this is usually what you want in the end because GUIs are usually turned into apps for ease of use. You can read the py2app documentation here, or read a non-official but easier to understand (in my opinion) tutorial here. I will also sum up how to do the process.
First install py2app:
Enter this into the command prompt:
sudo pip install -U py2app
If successful, you should get py2app.
If not, one problem might be you don’t have pip. You can download it with another command:
sudo easy_install pip
Step one:
Create a file called setup.py in the same dictionary
as the program.
Step two:
Put this into the file.
from setuptools import setup
#APP would be the name of the file your code is in.
APP = ['example.py']
DATA_FILES = []
#The Magic is in OPTIONS.
OPTIONS = {
'argv_emulation': False,
'iconfile': 'app.icns', #change app.icns to the image file name!!!
}
setup(
app=APP,
name='Your app’s name', #change to anything
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
)
Step 3:
Then open the bash terminal in the dictionary the file is in and type this command:
python setup.py py2app -A
The -A makes the app respond to updates in the code, but makes the app unsharable. When you are done developing, rerun the command, this time without the -A, like so:
python setup.py py2app
Note: You may need to use the command python3 setup.py py2... instead of python setup.py py2... for a python 3 py2app.
Step 4:
Navigate to the dictionary your code is in/dist. In that folder will be your app. (The dist folder should have been created in step three when you ran the command)
For windows users: py2app is not what should be used, instead use py2exe.
I found a solution that worked for me, changing the application icon rather than the window icon using the pyobjc module.
import tkinter as tk
import sys
root = tk.Tk()
if sys.platform.startswith('darwin'):
try:
from Cocoa import NSApplication, NSImage
except ImportError:
print('Unable to import pyobjc modules')
else:
ns_application = NSApplication.sharedApplication()
logo_ns_image = NSImage.alloc().initByReferencingFile_('/path/to/icon.icns')
ns_application.setApplicationIconImage_(logo_ns_image)
else:
pass # handle other platforms
root.mainloop()
tkinter.iconbitmap creates a proxy icon on mac which is a shortcut to that file. If you right click a file and select get info, the window that popes up has an icon. It is the icon of the file. You can drag the icon, and that will move the file. That is a proxy icon. If you set the iconbitmap, with an .app file the proxy icon will be that app. Since files dragged from the applications folder create shortcuts, if the .app you set iconbitmap to be, it will make a short cut if you drag the icon of the title bar. This is helpful because you don't want the user to be able to just drag your icon file out of it's directory, so it doesn't work next time your tkinter program loads that .app file. Before you convert your tkinter program to an app add root.iconbitmap("/Applications/<appname>.app").
Then move your app to the applications folder. When you open your app your tkinter window will have the icon of your app.If you drag the icon into a different folder it will create a short cut to your app.

Drag and Drop Files into Folders via Python GUI?

Problem
I'm trying to make a gui in python that displays two directories in two side-by-side panels and their contents (via tree, thumbnails, list, etc), which then allows the user to either:
drag files between both panels
select files to transfer (manually and via script), and transfer after user input
The point is to automate as much of the process as possible, but allow the user to verify each file's final transfer.
Is this possible? Which python gui library would be ideal for this? I'm just looking for a general direction, since I really don't know where to begin looking.
Current workaround
I have a python script that will sort mail into subfolders based on client, and then iterates through each client, simultaneously opening mail\<client> and the client's main file directory in explorer. I drag each file to its appropriate <main>\<client\<subfolder>, close the two windows, press 'enter' in IDLE, and it moves on to the next client. Tedious, but it's a rough implementation.
System info
Windows XP, Python 2.7
Well, you could implement a custom drag-and-drop with any GUI framework, so you might consider using Tkinter, since it's built-in to the standard Python library.
If you need to be able to drag-and-drop between applications, it's a bit more complicated, but supposedly can be achieved with Tkinter, although you might prefer to use wxPython, for which there's an example on the wiki.
For anything file related I would use the Python os module, http://docs.python.org/2/library/os.html

py2exe setup.py with icons

How do I make icons for my exe file when compiling my Python program?
I was searching for this a while ago, and found this: http://www.mail-archive.com/pygame-users#seul.org/msg05619.html
Quote from above link:
The setup.py File: PY_PROG =
'trek10.py' APP_NAME = 'Trek_Game'
cfg = {
'name':APP_NAME,
'version':'1.0',
'description':'',
'author':'',
'author_email':'',
'url':'',
'py2exe.target':'',
'py2exe.icon':'icon.ico', #64x64
'py2exe.binary':APP_NAME, #leave off the .exe, it will be added
'py2app.target':'',
'py2app.icon':'icon.icns', #128x128
'cx_freeze.cmd':'~/src/cx_Freeze-3.0.3/FreezePython',
'cx_freeze.target':'',
'cx_freeze.binary':APP_NAME,
}
--snip--
Linking the icons is answered in other answers. Creating the thing is as easy as using png2ico. It creates an ico file from 1 or more png's and handles multiple sizes etc, like:
png2ico myicon.ico logo16x16.png logo32x32.png
Will create myicon.ico with sizes 16x16 and 32x32. Sizes must be multiples of 8 squares, and no larger than 256x256.
py2exe is a little dated, and has been continued with pyinstaller (which itself is a little dated; the svn release is the most up to date)
http://pyinstaller.python-hosting.com/
After running through the initial scripts for pyinstaller and generating the spec file from Makespec.py, edit the spec file and look for the EXE section. At the tail end of that just add in your ico definition; so
console=True)
would become
console=True, icon='mine.ico' )
That is, if the mine.ico file were in the same folder as the Makespec.py file. There's also a command line option for feeding the icon into it. I think it was
python Makespec.py -i 'mine.ico' /path/to/file.py
I have no experience with py2exe but a quick google search found this, if embedding icons in exe files was what you asked for.
If you want to create .ico files, I'd really suggest you search for a icon designer or finished icons. Sure you can create a Win 3.x style icon fairly easy by creating a 16x16, 32x32, or 64x64 px image in paint, and rename it to .ico. But to create modern multi resolution icons for windows is a lot more complicated.
(I was about to ask what OS you was compiling for, when I realized "exe" sounds very windows, and sure enough...)

Categories

Resources