Which GUI approach in python

There is a good paper[1] talking about it. In the end, PySide sames to be a better choice. However, in the end, I choose the most reliable way to finish it. My choice is Tkinter. That is just because tkinter has best compatibility. I have tried it on Linux server, fedora desktop and mac. The application written with TK work all the way. However, when I try to use pyqt, gtk on the server, the necessary modules may not be installed by the root manager of that server, thus you will face difficulties to use it.

Tkinter GUI [2]

This is the first python GUI that I ever used. Its UI interface is ugly, however it is also the most supported UI designer on all versions of python.

A minimal example [1]

This example is provide in the reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import Tkinter as tk

class ExampleApp(tk.Frame):
''' An example application for TkInter. Instantiate
and call the run method to run. '''
def __init__(self, master):
# Initialize window using the parent's constructor
tk.Frame.__init__(self,
master,
width=300,
height=200)
# Set the title
self.master.title('TkInter Example')
# This allows the size specification to take effect
self.pack_propagate(0)
# We'll use the flexible pack layout manager
self.pack()
# The greeting selector
# Use a StringVar to access the selector's value
self.greeting_var = tk.StringVar()
self.greeting = tk.OptionMenu(self,
self.greeting_var,
'hello',
'goodbye',
'heyo')
self.greeting_var.set('hello')


# The recipient text entry control and its StringVar
self.recipient_var = tk.StringVar()
self.recipient = tk.Entry(self,
textvariable=self.recipient_var)
self.recipient_var.set('world')


# The go button
self.go_button = tk.Button(self,
text='Go',
command=self.print_out)
# Put the controls on the form
self.go_button.pack(fill=tk.X, side=tk.BOTTOM)
self.greeting.pack(fill=tk.X, side=tk.TOP)
self.recipient.pack(fill=tk.X, side=tk.TOP)


def print_out(self):
''' Print a greeting constructed
from the selections made by
the user. '''
print('%s, %s!' % (self.greeting_var.get().title(),
self.recipient_var.get()))


def run(self):
''' Run the app '''
self.mainloop()


app = ExampleApp(tk.Tk())
app.run()

Label display

1
2
3
# Label for EAST shot
Label_shot = tk.Label(self, text = 'shot', font = ("Arial", 16))
Label_shot.place(relx = 0.005, rely = 0)

relx and rely controls the relative position of the Label window
“place” property control where the Label will display in the main frame window.

Text input box

1
2
3
4
5
6
# Text input for shot number
self.recipient1_shot = tk.IntVar()
self.recipient1 = tk.Entry(self,textvariable=self.recipient1_shot,
font=("Arial", 16))
self.recipient1_shot.set('00000')
self.recipient1.place(relx = 0.08, rely = 0, relwidth = 0.08)

Get the return value of Text input box:
shot = numpy.float(self.recipient1_shot)

Check button

1
2
3
4
5
6
# Checkbutton for LHW all power
self.Check_LHW = tk.IntVar()
self.CB_LHW = tk.Checkbutton(self, text = "LHW all",
variable = self.Check_LHW, onvalue = 1, offvalue = 0,
font = ("Arial", font_size), justify = tk.RIGHT)
self.CB_LHW.place(relx = 0, rely = 0.3)

Get the return value of a check button:
on_off = self.Check_LHW = tk.IntVar()
If the check button is checked, on_off has value = 1. if it is not checked like the default case, the on_off value = 0. This value can be used for logical judgment.

Canvas figure window

Canvas will prepare an area to display figures inside the GUI main window.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# prepare a figure plot
fig = pyplot.figure()
ax = fig.add_subplot(111)
ax.plot(t, sig)
# a tk.DrawingArea
canvas = FigureCanvasTkAgg(fig, master = self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
toolbar = NavigationToolbar2TkAgg(canvas, self)
toolbar.update()
# Control plot area
# canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas._tkcanvas.place(relx = 0.21, rely = 0.01)
# canvas._tkcanvas.grid(row = 2, column = 1)

Notice here, the fig = pyplot() initial a figure object, ax = fig.add_plot(111) initial an axes object. The plot function can only be attributed to axes object, while canvas only takes figure object as an input.

Push button

Push button will initial a function to run.

1
2
3
4
5
6
7
8
9
10
    # design plot button
self.go_button = tk.Button(self,text='Plot',
command=lambda: self.plot_out(canvas,a),font=("Arial", font_size))
self.go_button.place(relx = 0., rely = 0.8)


def plot_out(self, canvas, ax) :
ax.clear()
ax.plot(t, sig)
canvas.draw()

In the code, command=lambda: self.plot_out(canvas.a) is the function to be called. self is the object variable that contents all property of this GUI window. as is the axes object for the plotted figure. canvas the window object for this axes plot. This kind of structure shows a very good programming design for the GUI code structure.

Final effect of the complete GUI window
EAST_heating_GUI

Reference

[1] https://www.pythoncentral.io/introduction-python-gui-development/

[2] http://robotic-controls.com/learn/python-guis/basics-tkinter-gui