Graphical User Interface with Python

From AstroEdWiki
Revision as of 19:29, 29 March 2018 by WikiSysop (talk | contribs)
Jump to navigation Jump to search

Python is inherently a programming language ideal for data analysis, and it works well by simply entering commands and seeing the results immediately. For longer programs, or ones that are used frequently, the code is usually written into a file and then executed from the command line or as an element of a more complex set of scripted operations. However many users prefer a graphical user interface or "GUI" where mouse clicks, drop-down menus, check boxes, images, and plots access with the mouse and sometimes the keyboard. Python has that too, through a selection of tools that can be added to your code.

The most developed core component for this purpose is the Tk/Tcl library. In its most recent versions it provides a very direct way of running your programs within a GUI that is attractive, matches the native machine windows in appearance, and is highly responsive. While this is not the only one, it is a solid starting point and may be all you need. Later in this tutorial we will show you how to use Python running in background behind a graphical interface in a browser window which is another technique that is growing in capability as the browsers themselves gain computing power. However, the Tk library will allow you to build free-standing graphical applications that will run on all platforms on which Python runs.


A Tk Tutorial

An excellent starting point is the Tk documentation tutorial which will take you step by step through how to use this library in Python and in other languages. Work through this one first, and then return here and we will illustrated it with a few examples to show you how to use it in Physics and Astronomy applications.

TkDocs Tutorial


Building a Program

In the tutorial you will see examples of code segments, and a few complete programs. Here, step by step, is how to make one that you can use as a template for something useful. First, you may see lines such as these at the beginning of a Tk program:

 from tkinter import *
 from tkinter import ttk

which add the needed modules to your Python system. When you use "from xxx import *" that takes all the parts of xxx and adds them with their intrinsic names. Usually this is not an issue for such major parts of Python as tk, but it could cause a problem by overwriting or changing the meaning of something you are already using. Also, even if that is not the case, you cannot immediately tell if a function or constant is from tk, ttk or the core Python. An example would be Tk itself. The next lines of a simple program started this way may be

 root = Tk()
 mainframe = ttk.Frame(root, padding=3 3 12 12")

and if we wanted to use a string inside the tkinter program we would have

 mystring = StringVar()

somewhere in the code. The function "StringVar()" is not in the core Python. It comes from tkinter. Without the leading "from tkinter import *" the "StringVar()" is not defined.

For long term maintenance, a better approach is to load the module and give it a short name this way:

 import tkinter as tk
 from tkinter import ttk

Now we have to use "tk" along with the component we are taking from tkinter, but the resulting code is much clearer. Let's complete a program that will convert feet to meters, following an example from the Tk website.

 def calculate(*args):
     try:
         value = float(feet.get())
         meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
     except ValueError:
         pass
   
 root = tk.Tk()
 root.title("Feet to Meters")
 mainframe = tk.ttk.Frame(root, padding="3 3 12 12")
 mainframe.grid(column=0, row=0, sticky=(tk.N, tk.W, tk.E, tk.S))
 mainframe.columnconfigure(0, weight=1)
 mainframe.rowconfigure(0, weight=1)
 feet = tk.StringVar()
 meters = tk.StringVar()