Hi, I'm trying to write a gui application using tkinter and matplotlib. I am having trouble with the layout. I'm leaving all the buttons in one frame and the plot in another frame (ie optionsframe = tk.Frame(self), plotsframe = tk.Frame(self)) in a attempt to divide the window into 2 (unequal) parts. However I cannot get the matplotlib figure to scale I tried adding "plotsframe.grid_columnconfigure(0, weight=1 )plotsframe.grid_rowconfigure(0, weight=1)" but it doesn't work. can someone tell me how to use the whitespace? I'm leaving the code for the page in question below
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# Plot import matplotlib import matplotlib.pyplot as plt from matplotlib import style from numpy import arange from math import * matplotlib.use( "TkAgg" ) style.use( 'fivethirtyeight' ) from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg from matplotlib.figure import Figure ## gui import tkinter as tk from tkinter import ttk LARGE_FONT = ( "Verdana" , 12 ) NORM_FONT = ( "Verdana" , 10 ) SMALL_FONT = ( "Verdana" , 18 ) ## texts Nsup = "Not supported yet! \n error: 010" def popupmsg(msg): popup = tk.Tk() popup.wm_title( "!" ) label = ttk.Label(popup, text = msg, font = NORM_FONT) label.pack(side = "top" , fill = "x" , pady = 10 ) b1 = ttk.Button(popup, text = "ok" , command = popup.destroy) b1.pack() popup.mainloop() #Plot Y = [[]] X = [[]] Lines = 3 Sel = 1 def calc(ite, height, width): trans = ite * - 500 for i in arange( 0 , 10 , 0.05 ): # handle more plots if ( len (X) < = ite): Y.append([]) X.append([]) # Maths a = (pi * i * 20 * width) - ( 90 ) + trans if a > = - 90 and a < = 270 : y = sin(radians(a)) + (i * 1.1 * (ite / 100 ) + 1 ) + (height) else : y = i * 1.1 * (ite / 100 ) + (height) # save datapoints X[ite].append(i) Y[ite].append(y) def draw(): for i in range ( 0 , Lines): calc(i, i, i + 1 ) # baseline class GUI(tk.Tk): def __init__( self , * args, * * kwargs): ## start up tk.Tk.__init__( self , * args, * * kwargs) ## init tkinter tk.Tk.iconbitmap( self , default = "icon.ico" ) tk.Tk.wm_title( self , "prog. Name" ) ## container container = tk.Frame( self ) #Frame = window # fills in space, if there is white space fill container.pack(side = "top" , fill = "both" , expand = True ) # min size, priority container.grid_rowconfigure( 0 , weight = 1 ) container.grid_columnconfigure( 0 , weight = 1 ) ## Menu bar menubar = tk.Menu(container) filemenu = tk.Menu(menubar, tearoff = 0 ) filemenu.add_command(label = "Save" , command = lambda :popupmsg(Nsup)) filemenu.add_separator() filemenu.add_command(label = "Exit" , command = quit) newmenu = tk.Menu(menubar, tearoff = 1 ) newmenu.add_command(label = "Add line" , command = lambda :popupmsg(Nsup)) newmenu.add_command(label = "del line" , command = lambda :popupmsg(Nsup)) menubar.add_cascade(label = "File" , menu = filemenu) ## show it menubar.add_cascade(label = "New" , menu = newmenu) tk.Tk.config( self , menu = menubar) ## end Menu bar self .frames = {} for F in (StartPage, MainP): frame = F(container, self ) self .frames[F] = frame # north, south, east, west frame.grid(row = 0 , column = 0 , sticky = "nsew" ) self .show_frame(StartPage) def show_frame( self , cont): frame = self .frames[cont] frame.tkraise() class StartPage(tk.Frame): def __init__( self , parent, controller): tk.Frame.__init__( self , parent) label = ttk.Label( self , text = "Start page?" , font = "LARGE_FONT" ) ## just defined the object label.grid(row = 1 , column = 1 ) button1 = ttk.Button( self , text = "ok" , command = lambda : controller.show_frame(MainP) ) button1.grid(row = 2 , column = 2 ) class MainP(tk.Frame): def __init__( self , parent, controller): tk.Frame.__init__( self , parent) f = Figure() a = f.add_subplot( 111 ) draw() for i in range ( len (X)): if i is not (Sel): a.plot(X[i],Y[i], color = "black" , linewidth = 0.6 ) if i is Sel: a.plot(X[i],Y[i], color = "blue" , linewidth = 1 ) ## set frame optionsframe = tk.Frame( self ) optionsframe.grid(row = 0 , column = 0 ) ## withing options frame button1 = ttk.Button(optionsframe, text = "+" ) button1.grid(row = 1 , column = 0 ) button2 = ttk.Button(optionsframe, text = "-" ) button2.grid(row = 1 , column = 1 ) button1 = ttk.Button(optionsframe, text = "<" ) button1.grid(row = 2 , column = 0 ) button2 = ttk.Button(optionsframe, text = ">" ) button2.grid(row = 2 , column = 1 ) scaleframe = tk.Frame(optionsframe) scaleframe.grid(row = 3 , column = 0 ) slabel1 = ttk.Label(scaleframe, text = " Amp" , font = "NORM_FONT" ) slabel1.grid(row = 3 , column = 0 ) slabel2 = ttk.Label(scaleframe, text = " Frq" , font = "SMALL_FONT" ) slabel2.grid(row = 3 , column = 1 ) slabel3 = ttk.Label(scaleframe, text = " Inc" , font = "NORM_FONT" ) slabel3.grid(row = 3 , column = 2 ) scale1 = tk.Scale(scaleframe, from_ = 100 , to = 0.1 ) scale1.grid(row = 4 , column = 0 ) scale2 = tk.Scale(scaleframe, from_ = 100 , to = 0 ) scale2.grid(row = 4 , column = 1 ) scale3 = tk.Scale(scaleframe, from_ = 100 , to = 0 ) scale3.grid(row = 4 , column = 2 ) ## end #show the plot plotsframe = tk.Frame( self ) plotsframe.grid(row = 0 , column = 1 ) plotsframe.grid_columnconfigure( 0 , weight = 1 ) plotsframe.grid_rowconfigure( 0 , weight = 1 ) canvas = FigureCanvasTkAgg(f,plotsframe) canvas.show() canvas.get_tk_widget().grid(row = 0 , column = 0 , rowspan = 2 , pady = 20 , padx = 10 , sticky = 'nsew' ) """ #show the plot canvas = FigureCanvasTkAgg(f,self) canvas.show() canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand = True) # for matplotlib toolbar toolbar = NavigationToolbar2TkAgg(canvas, self) toolbar.update() canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand = True) """ app = GUI() app.geometry( "1280x720" ) app.mainloop() |