I've created a few python scripts that I have to run every once in a while, but now I constantly have to type or search the location to the script in order to execute it. I have been looking for simple software to create an app with buttons, but without success. I have also seen "python?" scripts with an interface where you can select options with arrow keys, but also for this I have not found how it is done. Is there anyone who knows how I can make one of these that I can open the interface and select the script I want to execute? It would really save me a lot of time.
You can Tkinter to create the GUI, and for each button create a function that runs your python script like this:
# Create the button
button1 = Button \
(root, text='Start script1!', command=lambda: script1())
def script1():
os.system('python ~/path/to/script1.py')
def script2():
os.system('python ~/path/to/script2.py')
I recommend using PyQt because that framework is the powerful tool for building GUI on python and for running scripts from python code as system processes you can use subprocess module. I think you know how to use Google to find tutorials =) And also you can check the official page with GUI FAQ here. Thank you!
Related
There is a user created GUI which is an .exe that loads a DLL. This GUI has a bunch of sliders, check boxes etc. I would like to move two sliders at the same time using python automatically without me using my mouse to move the sliders. I would like to know the python modules that an be used to achieve this purpose.
You can try using SikuliX.
It can be used to automate mouse or keyboard actions based on pattern matching. It is originally developed for Java I think, but it can be used with Python.
Basically, anything you can do yourself manually can be done with it.
The developer seems to be pretty active, so you can easily seek help from him if you have issues.
I'm a starting programmer looking to make a simple text based RPG from scratch. I know there might be an easy tool to do this but I want as little handed to me as possible to use this project as a sort of learning possible. I've been using Python and so far I really like it (I'm willing to use Java or Javascript if absolutely necessary.)
My problem though is that right now I'm using the console to run the game but I'd prefer to run it as a standalone application (also so I can distribute it in like an .exe or similar). Is there some simple way I can do this? Everything is in Unicode, so it just needs to be able to display Unicode text (in-line preferably) and have some way to check for key presses (to type commands).
I've looked into Kivy, but it seems far beyond what I need and the text it displays is not in-line and must be displayed line by line. Plus it doesn't seem to be able to be exported to a single file.
Thanks for the help and remember I'm very much a newbie.
If you want a GUI in Python, you can use TkInter, which is fairly easy to learn (https://wiki.python.org/moin/TkInter).
However, if you want to make it an executable so you can share it then you have to use something like the following:
cx_freeze (http://cx-freeze.sourceforge.net/)
py2exe(http://www.py2exe.org)
PyInstaller(http://www.pyinstaller.org)
These will 'freeze' your Python scripts by including the interpreter and libraries in the .exe file. There's a lot of information in this previously asked question; How do i convert a Python program to a runnable .exe Windows program?
Here's a basic example of a text thing in tkinter:
from tkinter import *
root = Tk()
playerEntry = Entry(root)
textLabel = Label(root, justify=LEFT)
playerEntry.pack()
textLabel.pack()
def changeText(addText):
textLabel.config(text = textLabel["text"] + addText + "\n")
def get(event):
changeText(">>> %s" % playerEntry.get())
do_stuff()
playerEntry.delete(0, END)
def do_stuff():
changeText("Stuff is happening")
playerEntry.bind("<Return>", get)
root.mainloop()
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 have looked at similar questions that may answer my question but I am still very unclear on how to go about the following:
I can create programs to run in the Python Shell in Idle and I can also set up windows with widgets in Tkinter, but whatever I create in Tkinter is pointless because I cannot figure out how to take my Python Shell code and "wrap" it in the Tkinter GUI.
I have assumed that it cannot be done, and that entirely new code must be written to assist the language that is specific to Tkinter. I am very confused on how to create a well-rounded program without being left with just a GUI "skeleton" with random buttons, labels, entries, etc. and a Python program that is very unappealing and can only run in the ugly little Shell.
What you create with Tkinter is not pointless. It sounds to me like you're trying to compile a stand-alone program in Python, using the Tkinter library to provide the GUI. Once you have a script working, you can use a program to compile into a standalone program. Look into using py2app on a mac, or py2exe on Windows. Google them and see if that's what you're looking for.
Porting an application from command line to GUI might require some rework (depending on degree of interactivity you want to achieve).
Basically, in a GUI application, you build a few widgets (buttons...) at startup, and then perfom all your actions "on reaction" of user input. You typically do this by binding callbacks onto your widgets (button, input field), and then enter a mainloop (or eventloop).
You might read this chapter about events and binding.
If your application is mainly computing oriented, providing a gui with a "launch" button, and an output field is straightforward. If you perform some command line input, you can switch to widget input at low cost. More interactive apps will require to be architectured toward interaction capabilities.
I am planning to do the folliwing:
Create a PyGtk GUI (hardcoded, no Glade) with some widgets, and at the bottom of the screen put some sort of VTE (Virtual Terminal Emulator) from where I could manipulate the widgets, for example changing their attributes and calling their methods from the commandline.
The result would be similar to using AutoCAD's commands, only that I would be acting upon the GUI objects.
I have already found very few things about gtk.VteTerminal widget, but not only could not find a working example or make one myself, it also seem to be a system terminal, not a "current session" python terminal where I could run python commands and access GUI objects.
Any suggestion?
Thanks for reading
What you want exists already: GtkParasite. It's meant for debugging, but I'm sure if you wanted it to actually be a part of your application, you could adapt it.
I'm late to the party, but I had a similar problem.
Look here
Virtual Terminal Question
It's an option if you decide to do something different than what you might have already done.