I am trying to find the easiest (with shortest code) way of displaying a popup dialog window in Windows in my non-GUI Python 3.7 program. Basically, it tries to find and grab control of a window with a particular name. If it finds the window, it does its thing and all is good. If it doesn't find the window I'd like a wee popup message saying "Window not found". I know I can do this using PyQt5 or similar, but it is an awful lot of code just display a dialog!
You could do
import ctypes
mymessage = 'A message'
title = 'Popup window'
ctypes.windll.user32.MessageBoxA(0, mymessage, title, 0)
ctypes is part of the standard library, so this works on Windows without the need to install any packages.
A most simple way I could think of (not necessarily elegant) would be creating a .vbs file and calling MsgBox from there
MsgBox "Window not found!", 65, "Information"
Then call that file from Python using
>>> import os
>>> os.system("call popupmessage.vbs")
However, this is not cross-platform, but I assume you are using Windows.
Also keep in mind the directory in the os.system call is relative to your working directory unless you specify the full (or absolute) directory.
You should refer to https://www.tutorialspoint.com/vbscript/vbscript_dialog_boxes.htm on how to use MsgBox
Related
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.
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.
I am trying to create a simple file selection dialog for one of my scripts and I was trying to use the code examples from this thread: Quick and easy file dialog in Python?
import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
I also tried using the easygui module that uses Tkinter to do the same thing. In both cases, the code above hangs the IPython console. I gather that this has something to do with event loops, but I have no real experience with GUIs in Python.
Could someone point me in the right direction on how to get a dialogbox for file selection to work with IPython/Spyder. For the record, I am on Python 2.7.6 and IPython 2.4.1
Before running the code above, you need to set the right event loop (in this case Tk), as you correctly guessed.
To do that you need to run this command:
In [1]: %gui tk
and then run your code.
Note: To access the documentation about the %gui magic in Spyder, you need to place the cursor in front of %gui and press Ctrl+I, like this
In [1]: %gui<Ctrl+I>
I've been looking for a way to open a text file with a text editor I made with python, and assumed it had something to do with system arguments, so made a simple application which would write the arguments sent by the system to a text window, and used "open with" on the text file and application, but the only argument was the path of the application. Many questions similar to mine have been answered on here but none of the answers have worked for me. How would I do this?
Thanks for any responses.
(I'm using OS X 10.9.5, with python 2.7)
Tried code:
from Tkinter import *
import sys, time
root = Tk()
root.geometry('300x200')
text = Text(root)
text.pack(side=TOP, fill="both", expand=True)
text.insert(END, sys.argv)
for x in xrange(len(sys.argv)):
text.insert(END,sys.argv[x])
root.mainloop()
Displayed text:
['/Path/pyfe.app/Contents/Resources/file_opener.py']/Path/pyfe.app/Contents/Resources/file_opener.py
If I understand your question correctly, you are talking about opening a file with the Finder's Open with context menu when clicking on a file. If so, it's probably a duplicate of MacOSX - File extension associate with application - Programatically. The standard way is to create an OS X app bundle (for Python programs, you can use py2app to do that) and then set proper key type in its Info.plist. That's assuming your text editor is a true GUI app (uses Tkinter or Cocoa or whatever) and not just a program that runs in a shell terminal window (in Terminal.app for example). In that case, you might be able to create a simple wrapper app (even using AppleScript or Automator and modifying its Info.plist as above) to launch your Python program in a terminal window and pass in the file name from the open event. To properly handle multiple files opened at different times would require more work.
UPDATE: as you have clarified, you are using Python Tkinter and a real GUI app. The native OS X Tk implementation provides an Apple Event handler to allow you to process Apple Events like Open Document. The support is described in tcl terms in the Tcl/Tk documentation so you need to translate it to Python but it's very straightforward. Python's IDLE app has an example of one, see, for instance, addOpenEventSupport in macosxSupport.py. For simple apps using py2app, you could instead use py2app's argv emulation.
I published a detailed description at https://moosystems.com/articles/8-double-click-on-files-in-finder-to-open-them-in-your-python-and-tk-application.html.
As answered above you need to adapt your Info.plist file to tell OS X that your app can at least view the file types you want to process.
Then you can use py2app's argv emulator to access any files dragged on your app while it's not running.
Then install a Tk event handler to accept files while the app is already running.
Find the details in the linked article.
I'm looking for a cross-platform way of making my Python script process a file's path by implementing a drag n drop method. At the moment I manually go to the terminal and use the sys.argv method:
python myscript.py /Python/myfile.xls
However this is slow and "techy". Ideally I would a quick and interactive way of allowing a file be processed by my Python script. I primarily need this to work for Mac but cross-platform would be better.
If you want to use Tkinter, have a look at the Tkinter DnD binding from here http://klappnase.bubble.org/TkinterDnD/index.html
When run, the binding shows an example with a listbox that allows you to drag a file on to it.
Do you want to drag and drop the myfile.xls onto your python script within your file navigator ? Say Finder or whatever on Mac, Explorer on Win, Nautilus etc. ? In that case there will not be a simple cross-platform solution, given that you will have to hook into different software on different systems.
For a Mac specific solution try AppleScript - here is a sample
And for something Pythonic there is http://appscript.sourceforge.net/ , http://docs.python.org/library/macosa.html
Otherwise the solution is in the answer above. Use a custom GUI built in Tk, or wx or QT. You can look up their respective documentation for drag and drop, they do have cross-platform ways of doing it.
It'd be easiest to just write a small GUI with Tkinter or something similar and have the user select a file from within the GUI. Something along these lines:
import tkFileDialog
f = tkFileDialog.askopenfilename()
# Go on from there; f is a handle to the file that the user picked
I'm not aware of any cross platform methods to get a script to work with drag and drop, however. This is probably easier, though.