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.
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.
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.
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.