Porting a Python Shell Program to a Tkinter GUI - python

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.

Related

Is there a way to operate a tkinter GUI via script?

I'm trying to speed test my code for a tkinter GUI. Naturally, requiring humans to hit buttons would make speed testing inconsistent. I was thinking that I could just have a timed script run each callback sequentially, but some callbacks like image displays wouldn't be able to run without the GUI open. Now I'm looking for ways to run a script on a GUI that's already in its main loop. Is there a way to do this?
Python has a few modules for handling GUI automation but here is a pretty nice tutorial https://automatetheboringstuff.com/chapter18/.
Also with windows you should be able to simulate keystrokes and mouse movements using the win32 api https://sourceforge.net/projects/pywin32/.

interacting with a user created gui dynamically using python

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.

Executing a python script in a subprocess - with graphics

I've seen a lot of stuff about running code in subprocesses or threads, and using the multiprocessing and threading modules it's been really easy. However, doing this in a GUI adds an extra layer of complication.
From what I understand, the GUI classes don't like it if you try and manipulate them from multiple threads (or processes). The workaround is to send the data from whatever thread you created it in to the thread responsible for the graphics and then render it there.
Unfortunately, for the scenario I have in mind this is not an option: The gui I've created allows users to write their own plotting code which is then executed. This means I have no control over how they plot exactly, nor do I want to have it. (Update: these plots are displayed in separate windows and don't need to be embedded anywhere in the main GUI. What I want is for them to exist separated from the main GUI, without sharing any of the underlying stack of graphics libraries.)
So what I'm wondering now is
Is there some clean(ish) way of executing a string of python code in a whole new interpreter instance with its own ties to the windowing system?
In response to the comments:
The current application is set up as follows: A simple python script loads a wxPython gui (a wx.App). Using this gui users can set up a simulation, part of which involves creating a script in plain python that runs the simulation and post-processes the results (which usually involves making plots and displaying them). At the moment I'm doing this by simply calling exec() on the script code. This works fine, but the gui freezes while the simulation is running. I've experimented with running the embedded script in a subprocess, which also works fine, right up until you try to display the created graphs (usually using matplotlib's show()). At this point some library deep down in the stack of wxPython, wx, gtk etc starts complaining because you cannot manipulate it from multiple threads.
The set-up I would like to have is roughly the same, but instead of the embedded script sharing a GUI with the main application, I would like it to show graphics in an environment of its own.
And just to clarify:
This is not a question about "how do I do multithreading/multiprocessing" or even "how do I do multithreading/multiprocessing within a single wxpython gui". The question is how I can start a script from a gui that loads an entirely new gui. How do I get the window manager to see this script as an entirely separate application?
The easiest way would be to generate it in a temporary folder somewhere and then make a non-blocking call to the python interpreter, but this makes communication more difficult and it'd be quite hard to know when I could delete the temp files again. I was hoping there was a cleaner, dynamical way of doing this.
Can you simply use subprocess to run 'python.exe' and pipe the script in?
Alternatively, the multiprocessing package should suffice if you want to move some (pickle-able) data over to the new process in which you run the script. Just create a function/callable that runs the script, and create a Process object with the callable as target. That way, you should be able to pass some data over, without having GUI issues.
Capturing text with either is easy, subprocess allows that and no more. With multiprocess, you can pass python objects back and forth more easily.
On Windows, you can create window with a parent window from another process, and draw to that.
See the hWndParent argument to CreateWindowEx.
If wxWindows supports getting/setting that explicitly, then you should be good to go.
Depending on your platform, something similar might be possible in any windows system.
So, just giving your users the ability to find the handle of your apps window should give them the option to plot away at views embedded in your app, while running in their own processes.
I don't no much about wx, I work with jython(python implemented in java and you can use java) and swing. Swing has its own worker thread, and if you do gui updates you wrap your code into a runnable and invoke it with swing.invokelater.
You could see if wx has something like that, if you however are only allowed to manipulate the gui from the thread in which you created it try something similar. create a proxy object for your gui, which forwards all your calls to your thread which forwards them to the gui.
But proxying like this gets messy. how about you let them define classes, with an 'updateGui' function, that they should hand back to you over a queue and that you will execute in your gui thread.
In wxPython land when you use threads, you have to use its thread-safe methods to communicate with the GUI: wx.CallAfter, wx.CallLater or wx.PostEvent. In your case, I would run any long running code in a separate thread/process and when it's done its processing, send the result to the GUI. The GUI can instantiate a new frame and use matplotlib or PyPlot to show the plot, depending on which way you want to go. I've heard you can draw the plot using FloatCanvas too.
Anyway, if you instantiate the new frame correctly, then you can instantiate N frames and show them and be fine. See the wxPython wiki for a few examples of using Threads with wx: http://wiki.wxpython.org/LongRunningTasks

Embed interactive shell (VTE) in a PyGtk GUI to manipulate its own widgets

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.

How do I make a dialog box that waits for user response?

When you tkSimpleDialog.askinteger, the program stalls and waits for user input. What are the basics of writing my own method that would have the same effect? I want to make the same kind of dialog box, I just want to be able to request more information.
The problem that I'm having is that when I open the new window using Tk.Toplevel, the program does not wait for user input the way tkSimpleDialog.askinteger does.
First off, if you can use some other widget system like PyGtk or PyQt, you should seriously consider it. Tkinter is ancient, and the newer libraries have a lot more functionality (read: more things you don't have to reinvent). I've used PyGtk and like it a lot more than Tkinter, which I used in the old Python 1.x days.
That said, in Tkinter, you need to do this:
widget.wait_window(window)
This ties up the event loop waiting for the user to dismiss the dialog.
Reference: http://www.pythonware.com/library/tkinter/introduction/dialog-windows.htm

Categories

Resources