GUI examples with tk

Se show some example codes for GUI using Tk.

GUI including sliders, buttons

In [3]:
# -*- coding: utf-8 -*-
"""
Created on April 2017

This doesn't work if it's runninig on a remote host via network

@author: toyoki
"""

#  Derived from
#   chaos-3.py
#
#  Build Feigenbaum Logistic map. Input start and end K
#
#  python chaos-3.py 3.4 3.9  
#
from Tkinter import * #Tk, Canvas, Frame, Button, Scale
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib.lines import Line2D


class showMap(Frame):  # inheritate the Tk frame

    def __init__(self, master=None):
        self.frame = Frame(master)
        # plotting plane object
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        self.canvas = FigureCanvasTkAgg(self.fig, master=master)
        self.canvas.get_tk_widget().pack()
        self.frame.pack(side="top")
        self.run(3.5,3.9,0.0,1.0,300)
        
    def run (self, k1, k2, ymin, ymax, iteration) :
        self.ax.set_ylim(ymin, ymax)
        self.ax.set_xlim(k1,k2)
        
        x = .8                     # initial value of the map, which is somewhat arbitrary
        transient_range = range(100)
        iteration_range = range(iteration)
        k_step = (k2 -k1)/200  # unit of horizontal iteration
        k = k1
        while k <=k2 :
            for i in transient_range :
                x = x* (1-x) * k               # transient region is not drawn
            x_n = []
            y_n = []
            for i in iteration_range:
                x = x * (1-x) * k              # next x value
                y_n.append(x)
                x_n.append(k)
                if x <0 or x >= 1.0 :
                    print("overflow at k", k)
            self.ax.plot(x_n, y_n, "b.", markersize='1')
            k = k + k_step
        self.canvas.show() # to make a new plot visible
        return

class controlFrame() :
    """
    Control frame for setting parameters and control buttons
    """
    def setupControlBox (self) :
    
        # control buttons
        self.controlBox.StartBtn = Button(self.controlBox,text=u"start", width=100)
        self.controlBox.StartBtn.bind("<Button-1>", self.startApp)

        # sliders for parameter setting
        self.controlBox.set_k1 = Scale(self.controlBox, from_=1.8, to=3.9, resolution=0.01, orient=HORIZONTAL, label="min", length=500)
        self.controlBox.set_k2 = Scale(self.controlBox, from_=1.8, to=3.9, resolution=0.01, orient=HORIZONTAL, label="max", length=500)
        self.controlBox.set_k1.set(self.k1) # initial value
        self.controlBox.set_k2.set(self.k2)
        # vertical width
        self.controlBox.set_y1 = Scale(self.controlBox, from_=0.0, to=1.0, resolution=0.02, orient=HORIZONTAL, label="y_min", length=500)
        self.controlBox.set_y2 = Scale(self.controlBox, from_=0.0, to=1.0, resolution=0.02, orient=HORIZONTAL, label="y_max", length=500)
        self.controlBox.set_y1.set(self.y1) # initial value
        self.controlBox.set_y2.set(self.y2)
        # number of iterations
        self.controlBox.set_iter = Scale(self.controlBox, from_=300, to=5000, resolution=100, orient=HORIZONTAL, label="iteration", length=500)
        
        # pack widgets in the control frame
        self.controlBox.StartBtn.pack()
        self.controlBox.set_k1.pack()
        self.controlBox.set_k2.pack()
        self.controlBox.set_y1.pack()
        self.controlBox.set_y2.pack()
        self.controlBox.set_iter.pack()
        return
    
    def startApp (self, ev) :
        k1 = self.controlBox.set_k1.get()
        k2 = self.controlBox.set_k2.get()
        y1 = self.controlBox.set_y1.get()
        y2 = self.controlBox.set_y2.get()
        iteration = self.controlBox.set_iter.get()
        self.mapObj.run(k1,k2,y1,y2,iteration)
            
    # constructor of control panel
    def __init__(self, master=None, mapFrame=None, k1=3.5, k2=3.9, y1=0.0, y2=1.0, iteration=300):
        self.k1 =k1
        self.k2 =k2
        self.y1 =y1
        self.y2 =y2
        self.iteration =iteration
        self.controlBox = Frame(master)
        self.setupControlBox()
        self.controlBox.pack(side="bottom")
        self.mapObj=mapFrame
        
# end of controlBox class

if __name__ == "__main__" :
    root = Tk() # parent for all
    mapF = showMap(master=root)
    app = controlFrame(master=root, mapFrame=mapF)
    root.mainloop()      # Just wait for user to close graph

Giving parameters by textboxes

In [ ]:
# -*- coding: utf-8 -*-
"""
Created on April 2017

@author: toyoki
"""

#  Derived from
#   chaos-3.py
#
#  Build Feigenbaum Logistic map. Input start and end K
#
#  python chaos-3.py 3.4 3.9  
#
from Tkinter import * #Tk, Canvas, Frame, Button, Scale
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib.lines import Line2D


class showMap(Frame):  # inheritate the Tk frame

    def __init__(self, master=None):
        self.frame = Frame(master)
        # plotting plane object
        self.fig = Figure()
        self.ax = self.fig.add_subplot(111)
        self.canvas = FigureCanvasTkAgg(self.fig, master=master)
        self.canvas.get_tk_widget().pack()
        self.frame.pack(side="top")
        self.run(3.5,3.9,0.0,1.0,300)
        
    def run (self, k1, k2, ymin, ymax, iteration) :
        self.ax.set_ylim(ymin, ymax)
        self.ax.set_xlim(k1,k2)
        self.ax.clear()
        x = .8                     # initial value of the map, which is somewhat arbitrary
        transient_range = range(100)
        iteration_range = range(iteration)
        k_step = (k2 -k1)/200  # unit of horizontal iteration
        k = k1
        while k <=k2 :
            for i in transient_range :
                x = x* (1-x) * k               # transient region is not drawn
            x_n = []
            y_n = []
            for i in iteration_range:
                x = x * (1-x) * k              # next x value
                y_n.append(x)
                x_n.append(k)
                if x <0 or x >= 1.0 :
                    print "overflow at k", k
            self.ax.plot(x_n, y_n, "b.", markersize='1')
            k = k + k_step
        self.canvas.show() # to make a new plot visible
        return

class controlFrame() :
    """
    Control frame for setting parameters and control buttons
    """
    def setupControlBox (self) :
    
        # control buttons
        self.controlBox.StartBtn = Button(self.controlBox,text=u"start")
        self.controlBox.StartBtn.bind("<Button-1>", self.startApp)
        self.controlBox.ResetBtn = Button(self.controlBox,text=u"reset")
        self.controlBox.ResetBtn.bind("<Button-1>", self.resetApp)

        # sliders for parameter setting
        self.controlBox.Lx1 = Label(self.controlBox, text="x_min")
        self.controlBox.Lx2 = Label(self.controlBox, text="x_max")
        self.controlBox.set_k1 = Entry(self.controlBox)
        self.controlBox.set_k2 = Entry(self.controlBox, text="x_max")
        self.controlBox.set_k1.insert(END, self.k1) # initial value
        self.controlBox.set_k2.insert(END,self.k2)
        # vertical width
        self.controlBox.Ly1 = Label(self.controlBox, text="y_min")
        self.controlBox.Ly2 = Label(self.controlBox, text="y_max")
        self.controlBox.set_y1 = Entry(self.controlBox, text="y_min")
        self.controlBox.set_y2 = Entry(self.controlBox, text="y_max")
        self.controlBox.set_y1.insert(END,self.y1) # initial value
        self.controlBox.set_y2.insert(END,self.y2)
        # number of iterations
        self.controlBox.Liter = Label(self.controlBox, text="# of iterations")
        self.controlBox.set_iter = Entry(self.controlBox)
        self.controlBox.set_iter.insert(END, self.iteration)
        
        # pack widgets in the control frame
        self.controlBox.Lx1.grid(row=0, column=0)
        self.controlBox.set_k1.grid(row=0, column=1)
        self.controlBox.Lx2.grid(row=0, column=2)
        self.controlBox.set_k2.grid(row=0, column=3)
        self.controlBox.Ly1.grid(row=1, column=0)
        self.controlBox.set_y1.grid(row=1, column=1)
        self.controlBox.Ly2.grid(row=1, column=2)
        self.controlBox.set_y2.grid(row=1, column=3)
        self.controlBox.Liter.grid(row=2, column=0)
        self.controlBox.set_iter.grid(row=2, column=1)
        self.controlBox.StartBtn.grid(row=2, column=2)
        self.controlBox.ResetBtn.grid(row=2, column=3)

        return
    
    def startApp (self, ev) :
        k1 = float(self.controlBox.set_k1.get())
        k2 = float(self.controlBox.set_k2.get())
        y1 = float(self.controlBox.set_y1.get())
        y2 = float(self.controlBox.set_y2.get())
        iteration = int(self.controlBox.set_iter.get())
        print k1, k2,y1,y2, iteration
        self.mapObj.run(k1,k2,y1,y2,iteration)

    def resetApp(self, ev):
        
        self.mapObj.run(self.k1, self.k2, self.y1, self.y2, self.iteration)
        
    # constructor of control panel
    def __init__(self, master=None, mapFrame=None, k1=3.5, k2=3.9, y1=0.0, y2=1.0, iteration=300):
        self.k1 =k1
        self.k2 =k2
        self.y1 =y1
        self.y2 =y2
        self.iteration =iteration
        self.controlBox = Frame(master)
        self.setupControlBox()
        self.controlBox.pack(side="bottom")
        self.mapObj=mapFrame
        
# end of controlBox class

if __name__ == "__main__" :
    root = Tk() # parent for all
    mapF = showMap(master=root)
    app = controlFrame(master=root, mapFrame=mapF)
    root.mainloop()      # Just wait for user to close graph