Python Forum
tkinter how to use whitespace with grid
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter how to use whitespace with grid
#1
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

# 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()
Reply


Messages In This Thread
tkinter how to use whitespace with grid - by kiyoshi7 - Feb-06-2018, 05:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Centering and adding a push button to a grid window, TKinter Edward_ 15 5,202 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Draw a grid of Tkinter Canvas Rectangles MrTim 5 8,091 May-09-2021, 01:48 PM
Last Post: joe_momma
  Tkinter grid columnspan being ignored mntfr 6 6,889 Feb-01-2019, 06:01 PM
Last Post: mntfr
  Widget placement issues with tkinter grid thread 1 mgtheboss 2 4,404 Jan-09-2018, 03:59 PM
Last Post: SmokerX

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020